Results 1 to 10 of 10
  1. #1
    guavajuice is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default string comparison with "=" and ".equal"

    public class Test {
    public static void main(String[] args) {
    String s1 = "hello";
    String s2 = "hello";
    //String s1 = new String("hello");
    //String s2 = new String("hello");
    if(s1 == s2) System.out.println("true");
    }
    }

    and it printed true when i run the program. I thought s1 and s2 pointed at different memory address so it shouldn't have printed anything.

    on my 2nd try, s1 and s2 are both declared as new string(after un-commenting them, and comment out the previous declaration), then it didn't print true as i expected.

    how come it prints true on the first try?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guavajuice View Post
    public class Test {
    public static void main(String[] args) {
    String s1 = "hello";
    String s2 = "hello";
    //String s1 = new String("hello");
    //String s2 = new String("hello");
    if(s1 == s2) System.out.println("true");
    }
    }

    and it printed true when i run the program. I thought s1 and s2 pointed at different memory address so it shouldn't have printed anything.

    on my 2nd try, s1 and s2 are both declared as new string(after un-commenting them, and comment out the previous declaration), then it didn't print true as i expected.

    how come it prints true on the first try?
    Both the compiler and the JVM itself play a few tricks; String literals (the stuff between double quotes) are collected by the compiler and stored once in a .class file. The JVM loads class files and stores those literals in a pool of Strings, all unique. So in your first example there will be only one String object "hello" (stored in that String pool) and both s1 and s2 point to it.
    Both references are equals so s1 == s2 is true.

    In your second example there's still that single "hello" String in that pool but two different Strings have copied those bytes to their own internal buffers making two different Strings so s1 == s2 will be false.

    kind regards,

    Jos

  3. #3
    guavajuice is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Both the compiler and the JVM itself play a few tricks; String literals (the stuff between double quotes) are collected by the compiler and stored once in a .class file. The JVM loads class files and stores those literals in a pool of Strings, all unique. So in your first example there will be only one String object "hello" (stored in that String pool) and both s1 and s2 point to it.
    Both references are equals so s1 == s2 is true.

    In your second example there's still that single "hello" String in that pool but two different Strings have copied those bytes to their own internal buffers making two different Strings so s1 == s2 will be false.

    kind regards,

    Jos
    If i have

    String s1 = new String("hello");
    String s2 = s1;

    I believe i'm assigning s1 address to s2?

    if i then modify s2's values
    s2 = "world";

    does s1 value gets modified too?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guavajuice View Post
    If i have

    String s1 = new String("hello");
    String s2 = s1;

    I believe i'm assigning s1 address to s2?

    if i then modify s2's values
    s2 = "world";

    does s1 value gets modified too?
    Did you give it a try? Also have a look at the static System.identityHashCode( ... ) method and print out the raw hashcode values for both s1 and s2.

    kind regards,

    Jos

  5. #5
    guavajuice is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Did you give it a try? Also have a look at the static System.identityHashCode( ... ) method and print out the raw hashcode values for both s1 and s2.

    kind regards,

    Jos
    I did actually. it print out s2 = world and s1 stays as hello so s1 didn't get modified. so i proceed to get hashcode value


    String a = new String("hello");
    String b = a;
    System.out.println(System.identityHashCode(b)+","+ System.identityHashCode(a));
    b = "world";
    System.out.println(System.identityHashCode(b)+","+ System.identityHashCode(a));
    System.out.println(b + "," + a);

    the hash code for a and b were the same after i do String b = a
    here's what i get: 6718604,6718604

    after assign "world" to b, the hashcode for b changed, while a stayed the same.
    14577460,6718604

    the last print line gave me different values
    world, hello

    b and a had the same hashcode at first,which meant that a and b pointed to the same object right? so when i gave b a new value. how come a didnt get that ?
    Last edited by guavajuice; 04-22-2010 at 08:21 AM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guavajuice View Post
    b and a had the same hashcode at first,which meant that a and b pointed to the same object right? so when i gave b a new value. how come a didnt get that ?
    You assigned a new value to b so it changed; nothing happened to a so it stays the same. Have a look at this simple example:

    Java Code:
    int a= 42;
    int b= a; // both a and b are the same
    
    b= 54; // what do you expect a to be?
    kind regards,

    Jos

  7. #7
    guavajuice is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You assigned a new value to b so it changed; nothing happened to a so it stays the same. Have a look at this simple example:

    Java Code:
    int a= 42;
    int b= a; // both a and b are the same
    
    b= 54; // what do you expect a to be?
    kind regards,

    Jos
    based on what happened in the string test, b should be 52, while a stays as 42.

    if i have the method in
    public static void add(int x) {
    x+=10;
    }

    int a= 42;
    int b= a; // both a and b are the same

    class.add(b) // modify b value

    Then when i print a and b, i get 52 for both. it behaves as i thought, a and b point to the same address, and when value of b changes from 42 to 52, a also has a new value of 52.

    When i simply do b = 52, I interpreted it as "modifying the value at address that a and b point to. However, it's actually making a new object (indicated by a new hashcode). Am i understanding this right?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    No.
    You get 42 for each.
    They aren't pointing at the same memory, since they're primitives.

  9. #9
    guavajuice is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    ah. you are right.

    if a and b were objects
    obj b = a;
    b.modify()

    then a and b values will both get modified right?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guavajuice View Post
    ah. you are right.

    if a and b were objects
    obj b = a;
    b.modify()

    then a and b values will both get modified right?
    Yep, more exactly put: the single object pointed to (referred to) by both a and b is modified.

    kind regards,

    Jos

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •