• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}

The answer for this question is false true..
I understood why (s3+s4).equals(s4+s3) is true
But is s3 and s4 have same contents, I expect s4 to point to the same object on the heap as s3. there is no "new" assignment to s4. Then why does s3==s4 return a false value ??
 
Abbu Rahman
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i believe u know about how string litrals/constants are handled .

String s3 = s1.substring(4,7), s4 = s2.substring(0,3);

in this case the substring returns a "new" string every time and hence s1==s2; //false

hope that helps.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah..got it thnks..
I guess it it was just
String a= "ABC";
String b= "ABC";
then they would point to the same obj and a==b would be true.
But since we are calling string methods new obj is created.
Also, using operands (c=a+b) will also create a new object.
I think now its clear to me..
 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

But Swati keep in mind that String methods do not always create a new String object as seen in the following code:



Output : true.

Conclusion: The essence is that if the contents of the String object does not change then no new String Object is created else a new Object is created.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tanu,

Conclusion: The essence is that if the contents of the String object does not change then no new String Object is created else a new Object is created.


Regarding your quote above, I have a question.

I tried to compile the following code,


The output was,
false
abcdef abcd

false
abcdef abcdef

The above output shows that even though the contents of the String object
remain the same, it results in the creation of a new String.That is why, the false, when we check for s4==s5.

Correct me if I am wrong !
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tanu,

I am sorry for the last message, there was a coding mistake on my part which resulted in this output.
You are absolutely correct as to what you said in your conclusion.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, now ething is more clear!!
In your code Abdulla

String s4=new String("abdcdef");
String s5=s1.substring(0,7);

u can try replacing s1 by s4 or the other way round in above two lines..
both ways answer is true..so concept clear now!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic