• 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

Doubt in Strings

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above program displays the following output:
arit
amit
arit
false
true
My doubt is how the output for s2==s3 becomes false?
can you please explain
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhivya rajagopal wrote:
The above program displays the following output:
arit
amit
arit
false
true
My doubt is how the output for s2==s3 becomes false?
can you please explain

Because the == operator does not check whether the two Strings are identical, but whether they are the same object. The compiler and JVM have no way of knowing that the two Strings’ contents are the same, so they are different objects. Try adding .intern() after the replace() call.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhivya rajagopal wrote:My doubt is how the output for s2==s3 becomes false?
can you please explain


String is an object. When comparing objects, you should always use equals() unless, as Campbell said, you want to know if two objects are the the same object.

At it's worst (ie, if the object doesn't have its own equals() method) it will do the same thing as '==', but if, like String, it has some other meaning, it will do what you expect; '==' won't.

Winston

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help me understand why the System.out.println values are not coming completely



OutPut:
arit
arit
false
false
false
false
false
s1==s3
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look at this: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Check the relative order of precedence of + and ==. Now, what would you expect the result of "s2==s3:"+s2==s3 to be?
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:You need to look at this: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Check the relative order of precedence of + and ==. Now, what would you expect the result of "s2==s3:"+s2==s3 to be?



Thanks Mathew,

hence "s2==s3:Arit"==s2 will be false

Correct me if I am wrong
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhivya rajagopal wrote:
The above program displays the following output:
arit
amit
arit
false
true
My doubt is how the output for s2==s3 becomes false?
can you please explain



There is another concept here :
The function replace creates a new String instance with value "arit" and returns a reference which you are storing in the variable s2. For s3 and s4 however, 2 different String instances are not created. Its the same string literal which is referenced by 2 variables. You can read up on a concept called String literal pooling.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic