Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search the forums you will find this question has been asked and answered many times.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

But in both cases, the hashcode() of str1 and str2 returns the same.

Regards
Ganesan
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic