Results 1 to 13 of 13

Thread: About Wrapper Classes ...

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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)

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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);

  5. #5

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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?

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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???

  9. #9
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  10. #10

    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.

  11. #11

  12. #12

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  13. #13
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width