This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
You've actually got the first one backwards! In the first case, "==" returns false, because you're comparing two, distinct, newly-created String objects. "equals()" returns true, because those two String objects have the same contents.
In the second case, you're comparing two references to the same String object; it's created when the class is loaded and used twice in the code. The two reference point to the same object, so both "==" and "equals()" return true.
The first case wont deals with single object. whenever the compiler sees tha new operator, it will create one object of the corresponding class. so two objects, with the same content will be created in that case. that is why we got str1==str2 => false and str1.equals(str2)=> true
but in the second case, both of the references will point to the same String object, that is present in the String pool. so both of the comparisons will return true.
Thanks
Anoobkumar
Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Vinney Shanmugam
Ranch Hand
Joined: Aug 27, 2008
Posts: 104
posted
0
First ---> You are creating String objects and comparing the references. Secound ---> You are creating string variables and comparing it. It's like creating any primitive variable and comparing them. [ August 27, 2008: Message edited by: vinod ]
Brij Garg
Ranch Hand
Joined: Apr 29, 2008
Posts: 234
posted
0
Hi,
String str1 = new String("java");
Above statement will create two objects.
first object is refering to "java" . We dont have any reference to this object. second object is created because of the new operator in heap memory area whose content is "java" and reference is stored in str1.
String str1 = "java";
This statement will create one object on heap memory area at the time when class will be loaded. Reference to this object is stored in the string pool area. Now when the compiler will compile this program, this internal reference will be copied to str1 variable.
Whenever "java" string is used in the program, its internal reference from the memory pool area will be used.
Therefore if we are having two statements String str1 = "java"; String str2 = "java"; then internal reference from the memory pool area will be copied to both these string variables. That's why there is just one object and two reference variable.
Please correct me if I am wrong somewhere.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Originally posted by vinod: First ---> You are creating String objects and comparing the references. Secound ---> You are creating string variables and comparing it. It's like creating any primitive variable and comparing them.
Primitive? Strings aren't primitives. You need to use their equals() method or their compareTo() methods (or their case-insensitive versions) to check equality or for comparison. Ganesan has found the equals() method and is using it correctly.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by bittoo garg: Hi,
String str1 = new String("java");
Above statement will create two objects.
That's not quite true. As you said later the literal object ("java") is actually created when the class is loaded. It would still be created even if that statement was never executed. But that's probably getting too detailed for the beginner's forum and Campbell will probably tell me off (again!) [ August 27, 2008: Message edited by: Joanne Neal ]
Ganesan Ramakrishnan
Ranch Hand
Joined: Mar 18, 2008
Posts: 84
posted
0
Thanks all,
But in both cases, the hashcode() of str1 and str2 returns the same.
Regards Ganesan
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Originally posted by Joanne Neal: But that's probably getting too detailed for the beginner's forum and Campbell will probably tell me off (again!)
Stop getting paranoid, Joanne. If I thought it too detailed (it probably isn't) I would simply move it to intermediate and leave it to Rob to tell you off.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Ganesan Ramakrishnan: But in both cases, the hashcode() of str1 and str2 returns the same.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus