aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String  == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String  ==" Watch "String  ==" New topic
Author

String ==

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
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


Cheers,
Ram
Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
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

Joined: Oct 22, 2005
Posts: 2
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

Joined: Jul 10, 2001
Posts: 1934
i am very skeptical about s3==s4 returning a true. The equals will return a 'true' but == will not to my understnding of strings.


Kishore
SCJP, blog
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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

Joined: Oct 18, 2005
Posts: 51
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

Joined: Jul 10, 2001
Posts: 1934
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

Joined: Oct 15, 2005
Posts: 21
thangs you ,veesam sridhar,and any other guys.
I have the same doubt about String for some time
thanks


Regards<br />Mandy<br /> <br />-----------------<br />Thinking,Thinking,Thinking,<br />Before you do any action!!!
 
 
subject: String ==