File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help on == in comparing String and non-String objects using the toString() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help on == in comparing String and non-String objects using the toString() method" Watch "Help on == in comparing String and non-String objects using the toString() method" New topic
Author

Help on == in comparing String and non-String objects using the toString() method

Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Hi all,

Please look at this piece of code:

This will give output

Is it true that, in case of the Byte object you�re comparing references to two 'newly' created String objects (thus false) and in case of the String object you�re (somehow) comparing references to existing Strings objects? So what does this mean? That the toString() method for the String class simply returns a reference to the already existing String object?

Thanks for any further explanation on this.

Johan Pelgrim
The Netherlands
[ May 26, 2005: Message edited by: Johan Pelgrim ]

Johan Pelgrim, The Netherlands
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
According to the javadoc, myByte.toString behaves exactly like Byte.toString(byte) method which:

Returns a new String object representing the specified byte. The radix is assumed to be 10.


The operative word here would be "new". I don't have access to the source at the moment, so I only have their sun's word here, but that would explain why you got false before.


Also, according to the javadoc for String, description of the toString() method:

This object (which is already a string!) is itself returned.


So "this object" instead of a new reference to the String
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
According to the JSDK Documentation of String.toString() and Byte.toString() this is what happens:

String.toString() returns itself, that's to say a new String is not created. That is why String.toString()==String.toString() will evaluate to true.

While Byte.toString() will actually return what the static method String.toString(byte) returns, that's to say a new String and hence Byte.toString()==Byte.toString() will evaluate to false.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i have not seen any static method in javadocs of sun

i.e static method String.toString(byte)

if i m wrong pls correct me...and

so final conclsion is that .... String.tostring(byte) method will always create new string object on heap ..thats why both string object returning false on == test ??

pls give last confirmation of this post...

regards


Thanks and Regards, Amit Taneja
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by amit taneja:
i have not seen any static method in javadocs of sun

i.e static method String.toString(byte)

I think he meant String.valueOf(byte)

so final conclsion is that .... String.tostring(byte) method will always create new string object on heap ..thats why both string object returning false on == test ??

pls give last confirmation of this post...

regards


Not really. There are cases that it will return the same object, however this is an implimentation detail and is subject to change. There is no garuntee that the Strings returned will be the same or different objects.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
so what will we do if we get such question in exam

and what about increaseing knowlege about this tricks...

is we have to do research for that.....
i mean ..there is no std as u say that it changes from case to case..
Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Hi guys,

Thanks for your thoughts. I think I'll go with Timmy Marks' reply that the toString() method for the Byte object returns a new String object, where the toString() method for a String object returns a reference to itself (If it's intermittent I'll go mad )

Thanks again...

Preparing to take the test on Monday or Tuesday ;-)

Johan Pelgrim
The Netherlands
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
I think he meant String.valueOf(byte)


I think he meant Byte.toString(byte) since that's what it says in the javadoc (look at the toString() method. There is a link to Byte.toString(byte) which says explicitly that a new String object is returned.

P.S. Johan, good luck on the exam!
[ May 26, 2005: Message edited by: Timmy Marks ]
 
wood burning stoves
 
subject: Help on == in comparing String and non-String objects using the toString() method