wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Doubt in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Strings" Watch "Doubt in Strings" New topic
Author

Doubt in Strings

Dhivya rajagopal
Ranch Hand

Joined: Dec 15, 2010
Posts: 42

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

Joined: Oct 13, 2005
Posts: 39784
    
  28
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

Joined: Mar 17, 2011
Posts: 8186
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 62
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

Joined: Apr 06, 2010
Posts: 4460
    
    8

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

Joined: Nov 27, 2003
Posts: 62
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

Joined: Jul 03, 2011
Posts: 256
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Strings