• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Strings

 
Dhivya rajagopal
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate 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

 
Rajesh Nagaraju
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
 
Matthew Brown
Bartender
Posts: 4566
8
  • 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
 
Praveen Kumar M K
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic