• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== comparison

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Campbell
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was successful the second time. Bug reported.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
false in both case .
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
false for me to... in both cases
 
John Campbell
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic