aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() on Integer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() on Integer class" Watch "toString() on Integer class" New topic
Author

toString() on Integer class

Kayalvizhi Umashankar
Greenhorn

Joined: Aug 04, 2005
Posts: 25
Please explain the following

1. When i give

Integer i = new Integer(10);
Integer i1 = new Integer(10);
System.out.println(i.toString() == i1.toString());

Output is true

2. When i give

Integer i = new Integer(11);
Integer i1 = new Integer(11);
System.out.println(i.toString() == i1.toString());

Output is false

How come?


Kayal
Nibin Jacob Panicker
Greenhorn

Joined: Aug 16, 2005
Posts: 22
I wonder how you got 'true' for the first one.. it shouldnt be true as far as i know..
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
I'm getting false for both. Which version of Java are you using ?
I'm using Java 5.0

Thanks
Devi
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
This is the method toString in Integer class, hope it helps (Java 1.4.2_04)


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Am using java 1.4 ...i have got the same answer as Umashankar..


wonder why ??? its different in 1.5


Tx
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Not sure abt the xplanation!!!


Integer i = new Integer(11);
Integer i1 = new Integer(11);
// System.out.println(i.toString());
// System.out.println(i1.toString());
System.out.println(i.toString() == i1.toString());
System.out.println(11==11);

The first has false but the second has true

and if we uncomment the SOP statements we get the value of 11 for both..

???
Tx
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Don't worry why. Just learn not to compare strings with "==" (unless you know that they have been "interned").


[ August 29, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nibin Jacob Panicker
Greenhorn

Joined: Aug 16, 2005
Posts: 22
i tried in both jdk 1.5 and jdk 1.4.1 ..in both im getting the same output i.e false
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Look at the code that I posted before, it should be clear after you see how they handle toString method.
it's the same like:

Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 122

Freinds
I m using 1.5 and getting false for both.

The reason for that is we are comparing two string objects references which cannot be same because for both we are creating new objects og Integer.

We can use equals method to check their equality.

$@nj00


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
This question seems to have come up a lot lately. As the code David posted shows, any Integer objects created with a hard-coded literal between -3 and 10 inclusive will use the same object in the string pool. Thus, when you use toString(), you get a true result for those strings.

Don't ask me why they chose to implement it this way, I have no idea.

This has been talked about in two other threads as well:
Wrappers
valueOf( ) and toString( )
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Don't worry why. Just learn not to compare strings with "==" (unless you know that they have been "interned").
Bingo.
Radu Mantale
Greenhorn

Joined: Aug 29, 2005
Posts: 4
String objects must be compared with the help of equals() method (compares object state) and not with "==" (compares object identity). "==" does not compare the content of the two Strings instead it compares the addresses of the two Strings.
The explanation for this behaviour is that values greater than 10 are built from scratch .
........
case 10: return "10";
........
 
Don't get me started about those stupid light bulbs.
 
subject: toString() on Integer class