• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrapper class  RSS feed

 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In wrapper class
when we convert a string to integer object
String str="99";
Integer X=new Integer(str);
Now X is an object and if we compare with int a=99 where a is an variable
X==a

answer is false
how can we compare an object and a variable
please explain
 
Pete Letkeman
Ranch Foreman
Posts: 902
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code of what you speak. If you try it out you should see that it prints true and not false.

What is happening here is the Integer X is being auto unwrapped (reduced to an int) and then compared to the primitive int a.
When comparing objects you should use the equals method of the object e.g. stringA.equals(stringB).

If you compare using the == then you are checking to see if the to variables point to the same object in memory.
However if you are using one of the wrapper classes then auto wrapping and auto unwrapping happen.

The wrapper classes and primitives they represent  are:
  • Boolean wrapper class for boolean
  • Character wrapper class for char
  • Float wrapper class for float
  • Double wrapper class for double
  • Long wrapper class for long
  • Integer wrapper class of int
  • Short wrapper class for short
  • Byte wrapper class for byte

  • By default every object inherits methods from java.lang.object including equals, hashCode and more.
    If you need to compare the objects that you create then you will probably need to override the equals method in the class definition.
    If you override the equals method in the class definition then you should override the hashCode method in the class definition.
     
    Rob Spoor
    Sheriff
    Posts: 21133
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One thing to keep in mind is that if you're using == with a primitive and a wrapper object, the wrapper object will always be unboxed. That can give some surprising results. For instance, the following does not print false (which would be the case if the primitive was boxed), but instead throws a NullPointerException:
     
    SunnyKumar Singh
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wrapper class is used to converting primitive types into object and object into primitive types.

    There are 8 java wrapper classes.

    Integer
    Byte
    Short
    Long
    Boolean
    Float
    Double
    Character


    All the java wrapper classes are by default final and immutable.

    Autoboxing : Automatic conversion of primitive types to object in known as autoboxing.

    for example : int to Integer, double to double, etc.

    Unboxing : Automatic conversion of objects into primitive types is known as unboxing.

    for example : Integer to int, Double to double, etc....
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!