It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String problems" Watch "String problems" New topic
Author

String problems

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
class Another {
String s;
public Another(String a)
{
s=a;
}
public String toString()
{
return s;
}
public static void main(String ar[])
{

Another a=new Another("24");

/*1*/System.out.println(a.toString()==a.toString());//returns true
Byte b=new Byte("24");
/*2*/System.out.println(b.toString()==b.toString());//returns false

}
}
Why does the code at 1 prints true and the code at 2 prints false.This == for string is really confusing me,can anyone tell a good rule of thumb for this?
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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 ]

    Mag
    luco zhao
    Ranch Hand

    Joined: Apr 23, 2002
    Posts: 50
    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
    Mag Hoehme
    Ranch Hand

    Joined: Apr 07, 2002
    Posts: 194
    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.)
     
    wood burning stoves
     
    subject: String problems