• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

doubt about Strings from Whizlabs

 
Greenhorn
Posts: 23
  • 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");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3 = "arit";
String s4 = "arit";
String s2 = s1.replace('m','r');
System.out.println(s2 == s3);
System.out.println(s3 == s4);

My answer is arit amit true true , but the answer is arit amit false true.

What I understood is s2,s3 and s4 points to the same memory area. Anybody can explain me these memory area in detail ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s3 and s4 reference the same String object, because it is a String literal "arit" in the String pool. Therefore, s3 == s4 will return true.

s2 also references a String with a value "arit", but this is a different object, because the replace method creates a new instance at runtime. Therefore, s2 == s3 will return false.

See Strings, Literally.
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Objects,Strings == compares references.Not value
When we do ,String s2=s1.replace('m','r'); it returns a new String string..given to s2.
String s3="arit";
one more string given to s3.
both strings have same value ,but == sees references.which are different.
 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc weber and verma.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class praveen
{

public static void main(String args[])
{
String s1=new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3 = "arit";
String s4 = "arit";
String s2 = s1.replace('m','r');
System.out.println(s2.equals(s3));
System.out.println(s3 == s4);

}
}

output:
arit
amit
true
true
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I am wrong here, but doesn't this basically deal with where the object is located in memory?


From the book.....when you call String s = new String("amit"); A new string object is placed in heap memory. But when you call String t = "amit", there is no object contained on the heap, instead, you are creating a String literal contained in the String Pool Memory. So as someone previously said, when you use == you are comparing the references, since the references are each pointing to two different sets of memory which don't know about each other the answer is false.

Isn't this this real reason it returns false or am I off?
 
I've been selected to go to the moon! All thanks to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic