wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() 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()" Watch "toString()" New topic
Author

toString()

Swati Kadam
Ranch Hand

Joined: Mar 05, 2008
Posts: 39
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.....


SCJP / Preparing For SCWCD
Nhat Nguyen
Greenhorn

Joined: Jan 09, 2008
Posts: 9
"==" 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

Joined: Mar 03, 2008
Posts: 31
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

Joined: Feb 28, 2007
Posts: 274
Both b1.toString() and b21.toString() refer to two different objects, hence the == test is failing.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Can some one tell me why they are referring to different objects?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
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

Joined: Mar 10, 2008
Posts: 44
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

Joined: Dec 29, 2007
Posts: 129
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

Joined: Mar 15, 2008
Posts: 1
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

Joined: Feb 28, 2007
Posts: 274
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

Joined: Aug 31, 2007
Posts: 784

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 ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Daniel Chemko
Ranch Hand

Joined: Feb 27, 2008
Posts: 32
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString()
 
Similar Threads
Interesting Question
* Byte == comparision
Why is " == " on different strings with same content is returning "false"
byteRef.toString() == byteRef.toString() ?
toString( ) method