This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String ==

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test1 {
public static void main(String args[]) {

String s1 = new String("amit");
System.out.println(s1.replace('m','r')); --- arit
System.out.println(s1); --- amit
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3); -- false
System.out.println(s3==s4); -- true

}
}

In the above code not sure how s2 == s3 returns false

String s2 = s1.replace('m','r'); -- Isn't this equivalent to String s2 = "arit";

Please explain

Regards,
JP
 
Joan Pujol
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JP Ravi:
class Test1 {
public static void main(String args[]) {

String s1 = new String("amit");
System.out.println(s1.replace('m','r')); --- arit
System.out.println(s1); --- amit
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3); -- false
System.out.println(s3==s4); -- true

}
}

In the above code not sure how s2 == s3 returns false

String s2 = s1.replace('m','r'); -- Isn't this equivalent to String s2 = "arit";

Please explain

Regards,
JP


s2!=s3 because are two different instances. When you do operations with strings new instances are created. If you want to reuse instances you can use the intern() method.

s3==s4 because literals are cached
 
Guru Nath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back Ground:

There is a difference between == operator and String class equals() method.

String class equal method compares the content of the strings and returns true if content is equal.

== operator compares the reference address and return true if it's equal.

Answer:

Here s2 and s3 represent different objects with same content. So it returns false.
Note: s2.equals(s3) returns true.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am very skeptical about s3==s4 returning a true. The equals will return a 'true' but == will not to my understnding of strings.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i am very skeptical about s3==s4 returning a true. The equals will return a 'true' but == will not to my understnding of strings.



Here you are creating string literals...

When you are creating strin literals..there is an implicit call of intern fucntion...that checks whether there exists a string literal in the string pool that is the same as the one you are trying to create now...

If there exists one...then the reference of the same is returned...

A new string literal is not created...so both the references point to the same string literal reference

But if you are creating objects using new...then intern is not invoked implicitly...so 2 objects are created with same contents...

Hope you get an idea...

 
veesam sridhar
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

*literal strings will represent the same reference if they are created
1)in the same class and in the same package
2)in different classes within the same package
3)in different classes in different packages
4)using constant expressions computed at compile time
by explicitly using the intern() method and the resulting string is already in the string pool


*literal strings will represent different references if they are newly created at runtime


The following example helps some how... (and it shows how to use intern() method explicitly)

String str1 = "Lions and Tigers and Bears!";
String str2 = "Lions and Tigers and Bears!";
String str3 = str2;
String str4 = new String("Lions and Tigers and Bears!");
String str5 = " Oh my!";
String str6 = "Lions and Tigers and Bears! Oh my!";
String str7 = str1 + str5;
String str8 = (str1 +" Oh my!").intern();

Comparison output:
str1 == str2 -> true // the str2 literal existed ("interned")
str1 == str3 -> true // hold the same reference
str1 == str4 -> false // str4 explicitly created
str2 == str3 -> true // hold the same reference
str2 == str4 -> false // str4 explicitly created
str3 == str4 -> false // str4 explicitly created
str6 == str7 -> false // str7 computed at runtime
str6 == str8 -> true // explicit use of intern() at runtim
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both above posters.

This cert preparation is giving me new feel about the language(after many years of actual coding).
 
Mandy Hou
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thangs you ,veesam sridhar,and any other guys.
I have the same doubt about String for some time
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic