• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString()

 
Swati Kadam
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");

Returns false..
why false?
As both strings represents same value.....
 
Nhat Nguyen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"==" is used to compare 2 string objects.
In this case, to compare values between two strings, you have to use "equals" method.

 
John Wenn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it represents the same value, But == operator checks the Bits (Memory location of a Object when comparing each other) there it results the false to compare actual value within a object you need to use equals() method
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both b1.toString() and b21.toString() refer to two different objects, hence the == test is failing.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one tell me why they are referring to different objects?
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nabila Mohammad:
Can some one tell me why they are referring to different objects?


toString() creates a new object, if it is invoked on an non-String type.
 
Hamsagayathri Palanisamy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gitesh Ramchandani:


toString() creates a new object, if it is invoked on an non-String type.


Hi Gitesh,
your explanation is good. may i know why new onjects are not created
for toString() on String type & how it differs from non-String type.
can you please explain me once?

-Thanks & Regards,
Hamsa
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gitesh, the first time toString() is invoked, if it creates a String object, doesn't it go to the String Pool. So in the second time when it toString() is called, does a new object get created?
 
Srinivas Jilla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you modify the code to...

if(b1.toString().intern() == b1.toString().intern()) --> This will return true.


The intern function places the strings in the string constant pool rather than heap memory.
I suggest for more concept on strings refer Beginning Java Objects.pdf.

Thanks.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dean Jones:
Hi Gitesh, the first time toString() is invoked, if it creates a String object, doesn't it go to the String Pool. So in the second time when it toString() is called, does a new object get created?



The toString() method creates a new object every time, if and only if it is invoked on an non-String type.

Had it been a string, toString() will place the object in the string pool.

I would appreciate if any Bartender would throw some more light on toString() keeping in mind the above discussion. That will be appreciated a lot.

Thanks,
Gitesh
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gitesh
I am confused for toString() method.
Please explain once again the concept of toString() for String and non-String objects.


Regards
Ninad
[ March 17, 2008: Message edited by: Ninad Kulkarni ]
 
Daniel Chemko
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the JRE source code explains it all.

String.java:


.toString() method of Object was never designed as an API item for equivalence which is why Object includes the .equals() methods. All toString() is really useful for is giving developers / users interesting information about the internal state of the object. Do not attempt to discover any type of meaningful information from toString() unless you yourself designed the class for that purpose.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic