Hi Gautam, your toString() method returns a reference to the original "24" string, you've passed to the constructor. That is, "24" has the same reference as your member variable s. If you compare a.toString() with a.toString(), you do compare s to s, which always must be true, since they refer to the same object. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I did some research to answer your question why b.toString() == b.toString() returns false: Let's check out the stack trace:
So, in fact b.toString() == b.toString() compares two different String objects. You have essentially the same situation as in:
As a rule of thumb: (1) == vs. equals == always compares object references. Always check whether the variables point to the same object or not! The equals() method in the Object class does the same:
(2) String and wrapper classes The String class and the wrapper classes (Byte, Integer, Double, ...) have overwritten the equals() method and compare the content. Therefore:
(3) String literals However, with String literals there is a special case. Consider the following piece of code:
When the object that uses this code is created, it first creates a String object "abc" and stores it in its String pool. When a String reference is assigned a String literal, the it checks its String pool. If it finds that the String is already stored in its pool, it returns the reference to the existing string. If not, it stores the String literal as a new String in its pool and returns the reference of this String object. Thus in the above example, one object is created (the String "abc"), and both variables, s1 and s2, point to the very same String object. Therefore s1 == s2 returns true. Be aware if the creation of a new String object is involved:
This code creates three (!) objects:
String object "abc"
String object a
String object b
For a more detailed explanation of the String pool you may also like to check the API for String.intern() I hope that this helps. Please correct me if I got something wrong. [ April 26, 2002: Message edited by: Mag Hoehme ] [ April 26, 2002: Message edited by: Mag Hoehme ]
Hello Mag Your explanations are great. But I've also got one more question as I am a little confused. Do you mean when Byte.toString() is invoked, it will call String.valueOf ((int) value) which calls String.valueOf (int value, int radix) which calls Integer.toString(int i, int radix) which calls String Integer.toString (int i) then up to here, A NEW STRING IS CREATED??? And I cannot find String.valueOf (int value, int radix) method in java.lang.String(JDK1.3.1)? Thanks in advance.
Originally posted by Mag Hoehme: I did some research to answer your question why b.toString() == b.toString() returns false: Let's check out the stack trace:
So, in fact b.toString() == b.toString() compares two different String objects.
Great thanks,<br />Luco Zhao
Joined: Apr 07, 2002
Hi Luco, Thank you for pointing out the mistake! (String.valueOf () of course calls String Integer.toString(int i, int radix) and not String.valueOf (...). You may check the corrected posting.)