my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == comparison 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 "== comparison" Watch "== comparison" New topic
Author

== comparison

John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
Can anyone explain the reason behind these different outputs to me?



As per my understanding the toString() method creates a new String object and as == compares the memory addresses it should return false for both the scenarios. Please advice
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I believe you have found an error in the API documentation. The best way to guarantee that you get a unique object is to use the new operator.

The API doc for Byte.toString() says:

public static String toString(byte b)

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


But, you will not always get a new String object. This is the explanation:

Byte.toString() calls String.valueOf(int)

String.valueOf() calls Integer.toString(int,int) with the second argument = 10

Integer.toString(int,int) calls Integer.toString(int) when the second argument is 10

This is the code that is executed:

As you can see, 1 is a special case, always returning the String literal "1", which is in the string literal pool. 11 is the default case, returning a new String object each time.

You can consider the bug to be either in the code or in the API documentation. Perhaps you should report it to Sun.


Mike Gershman
SCJP 1.4, SCWCD in process
John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
Cheers Mike. Do you know how to report a bug at sun? I tried sending them an email at java3d-beta@sun.com, as mentioned in their website, but the mail bounced back!
John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
Was successful the second time. Bug reported.
parimelazhagan vinayagam
Greenhorn

Joined: Apr 19, 2004
Posts: 16
for me,It prints "false" for both the case.
I am using JDK1.3 Version.
Can You tell me which JDK Version u r using.
Thanks
Parimelazhagan.V
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Parimelazhagan:

I used version 1.5.0

Please check the source code for Integer.toString(int) and see if your version also has the optimizing code I posted above.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
false in both case .
Pooja Shankar
Greenhorn

Joined: Mar 29, 2005
Posts: 16
false for me to... in both cases
John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
Parimelazhagan,

I am using version 1.4.2_04 of jdk.

Mike,

I got the following reply from sun:

-------
Hi Parimal Sharma,

Thank you for submitting a bug using our bug submit page.

I have reviewed your bug report and could reproduce the error in JDK 1.4.2_04. However I tried this in JDK 5.0 and saw that this issue has been fixed. Please try using JDK 5.0 available from: -

http://java.sun.com/j2se/1.5.0/download.jsp

Thanks again for taking time to report this issue.

Regards,
Jitender
-------

as per them it should have been fixed for the version you are using!?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Parimal:

I rechecked my source directory and it is the 1.4.2 version.

I guess you were not the first person to find this bug.

One lesson from this is the danger of using == with String objects. In this case, even the Sun Java team made a mistake.
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi,
Suppose i got the same question in the SCJP, then what should i do,
i mean all the people who are taking the exam may not work with the latest version of JDK, so in that case, the person who is using old version may be wrong, what is the solution for this?
sri.
John Campbell
Ranch Hand

Joined: Mar 11, 2005
Posts: 53
It would be a tricky situation but if I were sitting the exam I'd go with 'false' for both the cases as that's what the answer should have been anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == comparison