aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
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 ??


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Abbu Rahman
Ranch Hand

Joined: Jan 05, 2004
Posts: 46
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

Joined: Aug 18, 2004
Posts: 121
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

Joined: Apr 05, 2004
Posts: 145
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

Joined: Jan 09, 2004
Posts: 225
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

Joined: Jan 09, 2004
Posts: 225
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

Joined: Aug 18, 2004
Posts: 121
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings