Results 1 to 7 of 7

Thread: Wrapper classes

  1. #1
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Wrapper classes

    Hi everyone,

    So I was reading about Wrapper classes in a book. It says these classes wrap a primitive value into an object, giving it a constructor and methods. Wrapper classess implement the Comparable interface. Then it gives an example of Integer wrapper class:

    Integer(int value) // Is this a constructor that wraps int into Integer object?
    int intValue () // I don't know what this step means?
    boolean equals(Object other)
    String toString ()
    int compareTo(Object other)

    Why wrapper classes implement Comparable interface? In other words, what's the point of the last three steps. Compare this integer object to what?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Was there a java source that you copied these bits and pieces of code from?
    Could you post the whole thing here. Its hard to understand what you've posted without its context.
    Java Code:
    Integer(int value)        // could be a Constructor that takes an int as arg
    int intValue ()             // could be a method that returns an int
    boolean equals(Object other)   // a method that takes an Object and returns a boolean
    String toString ()                 // a method that returns a String
    int compareTo(Object other)  // a method that takes an Object and returns an int

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Wrapper classes are useful to do things such as add primitives into a Collection. Implementing the comparable interface allows these collections to be sorted (using the Collections.sort())

  4. #4
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Was there a java source that you copied these bits and pieces of code from?
    Could you post the whole thing here. Its hard to understand what you've posted without its context.
    Java Code:
    Integer(int value)        // could be a Constructor that takes an int as arg
    int intValue ()             // could be a method that returns an int
    boolean equals(Object other)   // a method that takes an Object and returns a boolean
    String toString ()                 // a method that returns a String
    int compareTo(Object other)  // a method that takes an Object and returns an int
    Hi Norm, actually I didn't find any source code for this, and that is also the reason why I am very confused over this code.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This looks like methods and a constructor that comprises the Integer wrapper class. You can find its API here: Integer (Java Platform SE 6)
    Thanks, Fubarable. I don't think I found boolean equals(Object other) Is it a method that takes an Object and returns a boolean.. But how do you return a boolean? by comparing two objects, then determine true or false?
    String toString () returns a String object representing this Integer's value. Can you give me an example?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Look at the API for the Object class

    Quote Originally Posted by MIA6 View Post
    I don't think I found boolean equals(Object other)
    You'll want to look again. Remember that a class's public methods include all the public methods of its parent classes, and since this method is declared by the Object class, the parent of every Java class, you will need to become very familiar with it. As it happens Integer overrides Object's version of this method.

    Is it a method that takes an Object and returns a boolean.
    Of course.... that's how it's declared, right?

    But how do you return a boolean? by comparing two objects, then determine true or false?
    The API will tell you how it does this. Again, reread the API for Integer.

    String toString () returns a String object representing this Integer's value. Can you give me an example?
    What do you mean give you an example?
    This?
    Java Code:
    Integer myInteger = new Integer(5);
    System.out.println(myInteger.toString());  // toString is not really necessary here but illustrates the point

Similar Threads

  1. need help with do/while loop inc wrapper classes
    By afisher300 in forum New To Java
    Replies: 2
    Last Post: 05-14-2009, 10:06 AM
  2. pointers and wrapper classes
    By becky in forum New To Java
    Replies: 11
    Last Post: 02-07-2009, 04:59 AM
  3. What are wrapper classes?
    By maheshkanda in forum New To Java
    Replies: 1
    Last Post: 02-06-2009, 05:39 PM
  4. wrapper classes
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 12-11-2007, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •