Results 1 to 16 of 16
  1. #1
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Thumbs up Secret of String references comparision.

    //Just watch the following code snippet!
    class Sample
    {
    public static void main(String[] args)
    {
    //case 1
    String s1="Hello";
    String s2="Hello";
    if(s1==s2) //comparing the two object references
    System.out.println("equal");
    else
    System.out.println("!equal");
    //case 2
    String s3=new String("John");
    String s4=new String("John");
    if(s3==s4)
    System.out.println("equal");
    else
    System.out.println("!equal");
    //case 3
    String s5=s1;
    if(s5==s1)
    System.out.println("equal");
    else
    System.out.println("!equal");
    }
    }
    MyQuestion:-
    =========
    In case 3:obviously the two references s5 &s1 pointing to the same object hence it is printing "equal";known thing,Just leave it!
    Now,come to case 2:Here,the two objects s3 & s4 are referencing to two different string objects hence it prints "!equal";expected one.
    In case 1:the two objects s1 & s2 holds the same string "Hello".Here,Unlike case 2 it is printing "equal".what might be the reason can anybody help me?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It is no secret. It is the difference between a reference value and the value of the String.

    Read these two short documents (the first is mainly to get the terms used in the second).

    JavaRanch Campfire - Cup Size: a Story About Variables
    JavaRanch Campfire - Pass By Value, Please

  3. #3
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Yes boss!I read out the Articles,those are pretty good.
    I think,as soon as the statement String s1="Hello" executes by JVM,the JVM creates one 'String' object and stores that reference in s1.Next,while coming to the execution of String s2="Hello" ,Isn't it create the object of "string" just as did above?If not,how it assigns the reference of Object "s1" to Object "s2" and one more thing is why cant it be true for case 2?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because String literals, i.e. String values that you type directly into the code, exist only 1 time, and all reference to them use the same reference value.

    And, for case 2 you are calling "new String...", so a new string is created by copying the data from the string literal used to create it. So it is a brand new instance, therefore a new reference value.

  5. #5
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    oh!Great!Thank you Mr.Masijade.
    one more thing,
    while executing String s1="Hello"; Isn't it call the String class constructor by passing "Hello" as an Argument.If so,both the statements has to call the String class constructor by passing "Hello" as an argument there by both has to create the two separate String class objects na just as did in case 2?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No, all String literals are interned, i.e. they exist only once, so there is only, ever one reference value for them.

    Edit: And since I know you are going to ask why they are not the same in case 2, since that also uses a literal, I can tell you that the literal does exist, and it only exists once, but you are explicitly creating two new String instances copying the value of the String literal (the String value, not the reference value).
    Last edited by masijade; 02-23-2010 at 12:15 PM.

  7. #7
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    It means as soon as the statement String s1="Hello" executes JVM won't call a String class constructor,rather, it simply keeps the String "Hello" in the memory and returns the reference of it,which indeed stored inside the reference s1.so,next time when i use the same string it simply returns the existing reference which indeed stored inside the variable s2.If so,in case 2
    String s1=new String("Hello"); here also it should keep the String "Hello" in memory and returns the address of it then why can't it picks the existed reference of "Hello" created while executing String s1="Hello";

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because, as I said, you are explicitly telling it to create a new String with the same value. What do you not understand about that?

  9. #9
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    yeah!I understood!But in C++ you can call the argumented-constructor just as i did in case 1;String s1="Hello";
    //The compiler converts it to s1=new String("Hello");
    Exactly,here I'm getting confused.Isn't it be true in java as well.If so,the case 1 and case 2 doesn't make any difference na?

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, as I said, Java doesn't. All String literals are interned and exist only once! So, what part of that do you not understand. How it is done is completely irrelevant. It is not even (in the language specs) defined how it is to be done, only that it is to be done.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Java is not C++ is it?

  12. #12
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    ofcourse,it is not c++.Then,how it will interpret the statement String s1="Hello";?In c++ compiler interpret this one as i mentioned above i hope it too in java.That's it!

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  14. #14
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Hm mm!The Articles are awesome and now i find the answer for my question in these articles under the section "String Literals".Now i agreed with your answer "Strings formed with Constant string literal within the classes across the packages and with in the same package should share a common reference.We can also get the common reference explicitly by calling the method intern().
    like, String s1="Hello";
    String s2=("Hel"+lo).intern();//great

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Be careful with that, though, as interned strings use PermGen memory space in addition to standard heap space, and, normally, you only have a relatively small amount of PermGen space, so you don't want to go around interning all your Strings. It is also a very ineffecient process.

  16. #16
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. String comparision method problem
    By rons_sacramental in forum New To Java
    Replies: 7
    Last Post: 10-15-2009, 05:15 AM
  2. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM

Tags for this Thread

Posting Permissions

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