|
-
Sep 4th, 2001, 02:09 PM
#1
Thread Starter
Randalf the Red
About Wrapper Classes ...
I am really disappointed over this thing.
If everything in Java is classes, why have separate wrapper classes for the built-in data types? Why not embed the functionality in the int, float etc.?
The only possible reason I can think up of is that these intrinsic data types are really not implemented as classes, and so when they need to be used as objects, the wrapper classes are used.
Is there any other explanation? I have learnt only one use for the wrapper classes so far, and that is to change the type of a value from an intrinsic data type to an object.
.
-
Sep 4th, 2001, 03:13 PM
#2
Wrapper classes also contain conversion methods, like Integer.parseInt("327") converts the String "327" to the int 327.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Sep 4th, 2001, 05:02 PM
#3
Dazed Member
The only possible reason I can think up of is that these intrinsic data types are really not implemented as classes, and so when they need to be used as objects, the wrapper classes are used.
You are correct! Since primative data types are not classes
and cannot be accessed or instantized using methods
Wrapper classes are provided in the defalut package(java.lang)
-
Sep 4th, 2001, 05:10 PM
#4
Dazed Member
Just some methods and their uses.
Integer intObj1 = Integer.valueOf(String s);
returns a wrapper object corresponding to the to the primitive
value represented by the String object passed as an arguement.
Also as crptcblade pointed out. There are two parstInt() methods
within the Integer class. If you interested in numbering systems
public static int parseInt(String s);
public static int parseInt(String s, int radix);
you can also use:
public static String toHexString(int i);
-
Sep 5th, 2001, 04:30 AM
#5
Thread Starter
Randalf the Red
Well ...
Here is another thought.
For the purpose of converting numbers to objects and objects like strings to numbers, Java has wrapper classes. Why bother with the intrinsic data types then? Why not just implement all the int, float and other intrinsic variables as classes?
In C/C++, a string is essentially a character array. In Java it has been converted into a class with some added functionality. Similarly, Java could just implement int, float, double s as classes.
Or just like toString, Java could perhaps implement a method each for converting objects to the buiilt-in data types?
.
-
Sep 5th, 2001, 03:16 PM
#6
Dazed Member
In C/C++, a string is essentially a character array.
In Java it has been converted into a class with
some added functionality. Similarly, Java could
just implement int, float, double s as classes.
In my opinion this would be a pain in the ass
because i dont think it would be wise to always
represent primative types as objects.
What if you dont want or need the added
functionality? Why would you want to be forced
to use an Object representation of the primative
data type?. Especially when declaring variables
local to a method block(automatic).
Maybe the scope or life of the variable is
short lived?
As i see it Java treats Strings represented by
StringBuffers as a collection of characters.
Expandable....... I havent coded in C++ in a while
but why would you want to represent a
String as a char array?
-
Sep 5th, 2001, 03:31 PM
#7
Originally posted by Dilenger4
I havent coded in C++ in a while
but why would you want to represent a
String as a char array?
I think that is the only way in C. C++ added the string class.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Sep 5th, 2001, 07:23 PM
#8
Dazed Member
Isnt a string treated like a null terminated char array in C? I dont know i forgot. I used to like C++ but trying to do somthing quick took a little to long. Plus i couldnt stand Visual C++ with all of the added system code in green in your way. I mean what the **** is that ****. It like when you start a painting. You start with a blank canvas. Also who wants to deallocate your own memory???
-
Sep 6th, 2001, 10:01 AM
#9
Dazed Member
What difference does it make if I declared a variable to be int or Integer? It certainly wouldn't affect their scope. Maybe a bit more memory, but less pain.
More memory yes of course. But im not saying that it would
affect the variables scope. Why would you want to allocate
memory for an Object when a primative data type would
do just fine? I ment if the scope is short lived, like having a
small method. Plus i think speed is an issue when deallocating
memory for an object as opposed to a primative type.
-
Sep 6th, 2001, 03:18 PM
#10
Member
Re: About Wrapper Classes ...
Originally posted by honeybee
I am really disappointed over this thing.
If everything in Java is classes, why have separate wrapper classes for the built-in data types? Why not embed the functionality in the int, float etc.?
The only possible reason I can think up of is that these intrinsic data types are really not implemented as classes, and so when they need to be used as objects, the wrapper classes are used.
Is there any other explanation? I have learnt only one use for the wrapper classes so far, and that is to change the type of a value from an intrinsic data type to an object.
.
int, float, etc. are keywords. Integer, Float, etc. are classes with more functionality than the primitive data types.
-
Sep 6th, 2001, 03:48 PM
#11
Dazed Member
Just like Filbert...... Always adding his 2 cents in.
-
Sep 6th, 2001, 09:58 PM
#12
Thread Starter
Randalf the Red
Well ...
Originally posted by Dilenger4
More memory yes of course. But im not saying that it would
affect the variables scope. Why would you want to allocate
memory for an Object when a primative data type would
do just fine? I ment if the scope is short lived, like having a
small method. Plus i think speed is an issue when deallocating
memory for an object as opposed to a primative type.
That's reasonable.
But imagine a scenario where I have to convert numbers to strings or strings to numbers. I shall end up having a primitive data type as well as its wrapper class. With the waste of memory, it will clutter up the code and my head, too. I could afford a little extra memory to be wasted instead.
Of course when you move on to big things like internet apps, memory could be a valuable resource, and especially with Java's garbage collection system, you never know if the garbage was collected or not. In such cases, there is no option but to use both of them and get confused.
.
-
Sep 7th, 2001, 10:00 AM
#13
Dazed Member
I do see your point though. To convert a number to a string,you
would just use the toString(); method which is inherited from the
object class. But to convert back i guess you would have to use somthing like the getBytes() method from the String class. So you would end up having to allocate memory for a String Object and an integer variable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|