• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String and ==

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = new String("amit"); // allocated from heap
String s3="arit"; //allocated from String pool
String s4="arit"; // no new reference created
String s2 = s1.replace('m','r'); // AS "arit" ALREADY EXISTS, SHOULD IT NOT BE ALLOCATED REFERENCE OF s3 ?
System.out.println(s2==s3);
System.out.println(s3==s4);
WHY DOES IT PRINT "false" FOR FIRST SOP ?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is, the method replace() creates a new String object allocated from the heap.
In fact the allocation from String pool and subsequent optimization happens only in case of String literals. When we write a statement like,
String s3 = "arit";
s3 is assigned a reference to a String object stored in the String pool for the literal "arit". But in case of a statement like,
String s5 = new String("arit");
we are not assigning the variable s5 with a direct reference to String literal "arit". Here what happens is - a new String object is created with a value "arit" and s5 contains reference to this object. The allocation is from the heap and not from the pool. The same is true in case of replace() method.

Originally posted by Bharatesh H Kakamari:
String s1 = new String("amit"); // allocated from heap
String s3="arit"; //allocated from String pool
String s4="arit"; // no new reference created
String s2 = s1.replace('m','r'); // AS "arit" ALREADY EXISTS, SHOULD IT NOT BE ALLOCATED REFERENCE OF s3 ?
System.out.println(s2==s3);
System.out.println(s3==s4);
WHY DOES IT PRINT "false" FOR FIRST SOP ?


 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that when you write:
String s2 = s1.replace('m','r');
You are actually assigning the variable referece s1 to s2.
So as you said s3 is allocated from String pool is not equal to a reference variable allocated from heap.
cheers
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharathesh,
If you have doubts like this just go and refer the API. It helps you a lot. The replace() function returns
new String(0, len, buf);
This abstract is from the API..
Thanks & Regards,
Nijeesh.
 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I investigated few interesting things.. Maybe it would be old for u..
String s1 = new String("amit");
String s2 = "amit";
String s3 = s2.replace('m','m');
String s4 = s1.replace('m','m');
if (s2 == s3 )
System.out.println("True");
else
System.out.println("False");
if (s2 == s4)
System.out.println("True");
else
System.out.println("False");
the o/p of this is
True
False
So Ultimately it depends using which object reference you invoke the replace function..
If it is a reference which points to a string pool then the return value of the replace(if not modified) again points to the same reference.
Hope i am not confusing...

Thanks & Regards,
Nijeesh.
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Goutham, a hui, Nijesh BH
reply
    Bookmark Topic Watch Topic
  • New Topic