• Post Reply Bookmark Topic Watch Topic
  • New Topic

string compare  RSS feed

 
c mott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
    String one = new String ("bob");
    String two = new String ("bob");
    System.out.println (one == two);          // Why does this evaluate to: false?
   
    String s1 = "bob", s2 = "bob";
    System.out.println(s1 == s2);              // This properly evaluates to: true, as expected.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, for objects, including Strings, == tests for identity, not equality.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c moturu wrote:    String one = new String ("bob");
    String two = new String ("bob");
    System.out.println (one == two);          // Why does this evaluate to: false?
   
    String s1 = "bob", s2 = "bob";
    System.out.println(s1 == s2);              // This properly evaluates to: true, as expected.

In the first case you are comparing two references, not the contents that they are referring to, and because you've explicitly called new String to create the strings, the references will be different. You'd have to use one.equals(two) in order to compare the contents.

In the second case there's a little compiler magic that makes this true. The compiler sees two strings containing the constant "bob" and, to save storage, treats them as one and the same, hence comparing the references shows them to be equal. While the compiler is consistent with this behavior, if you rely on == to tell you they have the same content you're asking for a potential maintenance nightmare if some developer comes along after you and changes it to s1="bob, s2=new String("bob"). If you mean to compare the String contents, ALWAYS use the equals() method.
 
c mott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understand on the reference part being different but trying to see it being different - but didn't appear so - sorry, newbie Q..

    String one = new String ("bob");
    String two = new String ("bob");
    System.out.println (one + "  " + two + " " + (one == two));           // Resulting in: bob bob false
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you type String name = "bob" the compiler can optimize this by looking in a String pool to see if there is another "bob" already there.  If so, it will create a reference to the "bob" in the pool, not a new reference.

When you type String name = new String("bob") you are explicitly asking for a completely new String.  The compiler does not optimize it.
 
c mott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some search, the following may be the way to get reference ID of a string (at the last line) -

    String one = new String ("bob");
    String two = new String ("bob");
    System.out.println (one + "  " + two + " " + (one == two));
    System.out.println (Integer.toHexString(System.identityHashCode(one)) + "  " + Integer.toHexString(System.identityHashCode(two)));
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The identityHashCode is not the address. Please see the answer to this POST.
 
c mott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank each of you - for clarity on the comparison of object to do with their reference instead of value and on hash store and related compiler optimizations.  That helped.  Thanks.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch

We have an old JavaRanch journal article about that sort of question: try here. It might be old, but most of what it says is still useful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!