• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString() on Integer class

 
Kayalvizhi Umashankar
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder how you got 'true' for the first one.. it shouldnt be true as far as i know..
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the method toString in Integer class, hope it helps (Java 1.4.2_04)
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nibin Jacob Panicker
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan Kade
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry why. Just learn not to compare strings with "==" (unless you know that they have been "interned").
Bingo.
 
Radu Mantale
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";
........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic