• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

why == operator behaves differently

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


knowledge - equals() and == behaves similarly on the context of string comparision that's it check for the content of the Strings being compared


line 1 -
is true because the content of both the s and s2 are same.
line 2 -
shouldn't it return true aswell?
 
Marshal
Posts: 5467
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a little Wiki page all about that: AvoidTheEqualityOperator.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The == operator is used to check if two references are pointing to the same object. It doesn't matter if two different objects have the same value (ie. two strings objects that are equal), if they are two different objects, then the operator will return false.

With line 1, they are referring to the same object, because of the use of the string pool.

With line 2, they are two objects with the same value. It is the same value because the same replacement was made. However, the replace() method had to create a new object for the replacement with both calls. Hence, they are now two different objects.

Henry
 
Sheriff
Posts: 8515
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to guys, when you create a String s = "siva", Java looks in a String constant pool for the same value to reuse it, if it cannot find, it creates "siva" String object and places to a pool. It is done in this way in order to manage memory more efficiently.

When you create String object like String s1 = new String("siva") it creates object in a Heap memory. If you create another String s2 = new String("siva") it is going to create another String object in a Heap memory and these will never point to the same memory location. So s1 == s2 will be false, as == checks if these two reference variables refering to the same memory location.

In your way, you can think about it as:

I hope I didn't mess up.

[edit] fixed typos
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s sivaraman wrote:. . .
knowledge - equals() and == behaves similarly on the context of string comparision that's it check for the content of the Strings being compared


. . .Where did you get that from? (You should always quote sources.) That is possibly correct for some languages (e.g. C#, where the == operator is overloaded), but is not true for Java┬«.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:In addition to guys, when you create a String s = "siva", Java looks in a String constant pool for the same value to reuse it, if it cannot find, it creates "siva" String object and places to a pool. It is done in this way in order to manage memory more efficiently.



i missed this part so that caused the confusion.

thanks guys.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic