• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

About String == String, please help.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all, please refer to the following code:

The answer is <code>False</code>, why?
Thanks in advance.
Guoqiao
And the question comes from:
<code> http://www.geocities.com/skmajji/Main.html
</code>
Not bad questions there.
[This message has been edited by Guoqiao Sun (edited July 27, 2001).]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, because each method toString() creates a new String in the String pool(because String(s) are not mutable as StringBuffer(s)).
== cheks if REFERENCES to objects are equal. Thus, the answer is False. However, if you use equals() instead of ==, you will get True.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guoqiao,
The reason is that the <code> toString() </code> method of <code>Object</code> class returns a new Object of the string representation of the object being passed as an argument.
Generally most classes overload this method as per their specific requirement. In the case of String class this is not
provided for, so the Object class's toString is called.
Further, the "==" operator checks the memory location of the
object and does a "shallow" comparison and returns true "if"
both the ojects have the same memory location else it returns
false. In the question you posted the toString() method returned
saperate string object which obviously have different memory locations and hence the result "false" is displayed at standard
console.
Hope this helps.
Ravindra Mohan.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ravindra for your detailed explanation.
Now i got it.
Guoqiao
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The reason is that the toString() method of Object class returns a new Object of the string representation of the object being passed as an argument.
Generally most classes overload this method as per their specific requirement. In the case of String class this is not
provided for, so the Object class's toString is called.


Actually, the String class does provide its own toString() method. But that really doesn't have any bearing on the code in the original question.
The original code is using a Byte object. The Byte class also provides its own toString() method. Byte's toString() method returns a new String object when it's called, so by calling byte.toString() (in the original code) twice, returns 2 different String objects. And given the nature of the == operator, the code will print False.
Also, calling toString() will not put a String object into the String pool. You have to call intern() on that object to do that. Interestingly enough (and logically), if you run the following code, you get a printout of True.

[CODE]public class TestByteString
{
public static void main(String args[])
{
Byte b1 = new Byte("127");
if(b1.toString().intern() == b1.toString().intern())
System.out.println("True");
else
System.out.println("False");
}
}[\code]
April
[This message has been edited by April.Johnson (edited July 27, 2001).]
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, calling toString() twice on the Byre object creates two different String objects.Hence == gives false.
Using String.intern(), would not allow to create another instance of the String when byte.toString() is called the second time.Hence, it prints true in the output.
- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic