aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes More on the 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 "More on the String" Watch "More on the String" New topic
Author

More on the String

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
The following are mock exam (www.theglobe.com)questions:
1.
String str5 = new String("amit"); //line 1
String str7 = "arit"; //line 2
String str6 = str5.replace('m','r'); //line 3
System.out.println(str6 == str7);
The answer is false. I am confused because I think the string "arit" at line 2 in the string pool is the same one created at line 3. Please help!
2.
String s1 = "ab";
String s2 = "abcd";
String s3 = "cd";
String s4 = s1+s3;
s1 = s4;
if (s1 == s2) System.out.println("s1 == s2");
Again, the anwser is false. I think the concept in question 2 is similar to that of question 1. I am stuck on the same wrong concept. Need help.
Cq
Greenhorn

Joined: Nov 13, 2000
Posts: 8
this question has been asked again and again.
check the previous answer.
== and equals() is different.
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
Here is the explanation.
Answer for question 1:
String str5 = new String("amit"); //line 1
String str7 = "arit"; //line 2
String str6 = str5.replace('m','r'); //line 3
System.out.println(str6 == str7);
If any method of the java.lang.String class (like replace, concat etc) changes the contents of the String object then it results in a new object.
In line 2, str7 refers to a String object "arit".
In line 3, the replace() method changes the contents of the String object str5 and is assigned to str6. str6 refers to a new object and hence the memory address of str6 is different from that of str7.
Points to remember:
1.The "==" operation on any two String objects checks if the memory address of both the String objects being compared are equal or not.
2.The "equals()" method on two String objects checks if the contents of the two String objects being compared are equal or not.
So the statement, "System.out.println(str6 == str7);" returns "false" as the memory address of the two String objects str6 and str7 are different.
You may try, "System.out.println(str6.equals(str7));" - This returns "true" as the contents of the two string objects str6 and str7 are equal.
Question 2 is also based on the same concept mentioned above.
FYI:
Example1:
String str5 = new String("amit"); //line 1
String str7 = "arit"; //line 2
String str6 = str5.replace('m','r'); //line 3
String str7=str6;
System.out.println(str6 == str7);
This will return "true" as str7 refers to the same object refered to by str6, i.e, str7 and str6 have the same memory address.
Example 2:
String str5 = new String("amit"); //line 1
String str7 = "arit"; //line 2
String str6 = str5.replace('m','m'); //line 3
System.out.println(str5 == str6);
This returns "true" as the original String object str5 remains unchanged by the method "str5.replace('m','m').
- Suresh Selvaraj


Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Hi, Suresh, thank you for your explanation and patience. Then
why the following code:
StringBuffer sbf1 = new StringBuffer("Amit");
StringBuffer sbf2 = new StringBuffer("Amit");
System.out.println(sbf1.equals(sbf2));
gives the answer "false"? Please bear with me.
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
The "==" and "equals()" method on StringBuffer has the same effect i.e, both check for the equality of memory address of two StringBuffer objects being compared.
Example 1:
StringBuffer sbf1 = new StringBuffer("Amit");
StringBuffer sbf2 = new StringBuffer("Amit");
Both, System.out.println(sbf1.equals(sbf2)); and
System.out.println(sbf1==(sbf2)); will return "false".
Example 2: sbf2 is assigned to sbf1,
sbf1=sbf2
System.out.println(sbf1.equals(sbf2));
System.out.println(sbf1==(sbf2));
Both the above mentioned checks on StringBuffer object will return "true".
- Suresh Selvaraj
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Hi, Suresh, I appreciate your clarification. Thanks.
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
hi,
If you want to get the effect of equals() method then call toString() method before comparing.i.e
if (s1.toString().equals(s2.toString())){
System.out.println("equals");
}
This will give correct answer in synchronous with String's, equals() method
The example is :
public class SB{
public static void main(String[] args){
StringBuffer s1=new StringBuffer("javaranch");
StringBuffer s2=new StringBuffer("javaranch");

if(s1==s2){
System.out.println("==");
}

if (s1.toString().equals(s2.toString())){
System.out.println("equals");
}

}
}
HTH
-Prasad Ballari
------------------
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: More on the String