aspose file tools
The moose likes Beginning Java and the fly likes Doubt in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 26720
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: 1605

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


More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
Rajesh Nagaraju
Ranch Hand

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

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: 41
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: 148
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.
 
IntelliJ Java IDE
 
subject: Doubt in Strings
 
Threads others viewed
String consant pool
about string
one more on ==
doubt about Strings from Whizlabs
Doubt with String method "replace"
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture