Results 1 to 16 of 16
- 02-23-2010, 10:05 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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?
- 02-23-2010, 10:18 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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
- 02-23-2010, 11:39 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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?
- 02-23-2010, 12:00 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-23-2010, 12:09 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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?
- 02-23-2010, 12:12 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-23-2010, 12:29 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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";
- 02-23-2010, 12:40 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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?
- 02-23-2010, 01:18 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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?
- 02-23-2010, 01:36 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-23-2010, 01:37 PM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Java is not C++ is it?
- 02-23-2010, 02:42 PM #12
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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!
- 02-23-2010, 05:28 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 02-24-2010, 09:13 AM #14
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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
- 02-24-2010, 11:51 AM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-24-2010, 02:32 PM #16
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
String comparision method problem
By rons_sacramental in forum New To JavaReplies: 7Last Post: 10-15-2009, 05:15 AM -
comparision between two lists
By suprabha in forum Advanced JavaReplies: 14Last Post: 08-01-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks