Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Doubt in Strings

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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: 66975
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!