This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More on the String

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question has been asked again and again.
check the previous answer.
== and equals() is different.
 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Suresh, I appreciate your clarification. Thanks.
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic