File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is any difference in Integer Wrapper class and String class using boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is any difference in Integer Wrapper class and String class using boxing" Watch "Is any difference in Integer Wrapper class and String class using boxing" New topic
Author

Is any difference in Integer Wrapper class and String class using boxing

jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 60
Hi Gugy,

I didn't understand the Autoboxing feauture of this below code

Integer i = new Integer(1000);
int j= 1000;
if (i==j)
{
System.out.println("both are equal for Integer");
}
String s = new String("rsr");
String s1="rsr";
if(s==s1) {
System.out.println("both are equal FOR String");
}




when im trying to run ths code it prints
both are equal for Integer

and below is not printed , this is OK (i know what String Class does)
both are equal for string

but why the above one is printed(both are equal for Integer), Is any specific way to treat in Autoboxing


Thanks


Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
Try with "Integer j" in place of "int j" and notice the difference in behaviour.

In your code, when comparing the primitive with the wrapper it actually unboxes the wrapper to do a primitive v primitive comparison


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

When a primitive is compared with a wrapper, the wrapper is unboxed. As such, you are comparing the int 1000 with the int 1000.

Try turning i into null and then compare again:
And please, use code tags in the future.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Is any difference in Integer Wrapper class and String class using boxing
 
Similar Threads
String
intuitive equals exam q, help
String
Boxing n Auto boxing-marcus green
equals() in Wrapper Classes