permaculture playing cards*
The moose likes Beginning Java and the fly likes Difference between String s = Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between String s = "java" and String s = new String("java");" Watch "Difference between String s = "java" and String s = new String("java");" New topic
Author

Difference between String s = "java" and String s = new String("java");

Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 88
Hi,
We all known the .equals check the content of the string , but == check the object reference but i have one doubt in the following code,



another thing is



What is the difference between above two examples.

Regards
Ganesan

[ August 27, 2008: Message edited by: Ganesan Ramakrishnan ]
[ August 27, 2008: Message edited by: Ganesan Ramakrishnan ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3460
    
  12
If you search the forums you will find this question has been asked and answered many times.


Joanne
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 88
Thanks Ernest,

but how the second case pointing to the same object rather than like first case.

Regards
ganesan
[ August 27, 2008: Message edited by: Ganesan Ramakrishnan ]
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Hi Ganesan

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
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
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: 38436
    
  23
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: 3460
    
  12
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: 88
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: 38436
    
  23
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
Originally posted by Ganesan Ramakrishnan:
But in both cases, the hashcode() of str1 and str2 returns the same.


True. It has to be: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#hashCode()


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
 
jQuery in Action, 2nd edition
 
subject: Difference between String s = "java" and String s = new String("java");