File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String problems

 
Gautam Sewani
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 194
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    luco zhao
    Ranch Hand
    Posts: 50
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Mag Hoehme
    Ranch Hand
    Posts: 194
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.)
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic