• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Difference between == and equals

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I tried out the following code snippet:



Here :

a==b returns true

i1==i2 returns false

i1.equals(i2) returns true

My understanding of what is happening is as follows:

a) a==b returns true as it tests the value of the two integer primitives

b) i1==i2 returns false because although the types are same, they refer to different objects on heap.

c) i1.equals(i2) returns true because they are of same type and have same value.

Please correct me if I am wrong.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Correct



Thank you.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Thank you.


It's probably worth mentioning that these distinctions are blurred somewhat for the String and wrapper classes because they have CachedValues. It's generally best to AvoidTheEqualityOperator altogether when dealing with objects.

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Mansukhdeep Thind wrote:Thank you.


It's probably worth mentioning that these distinctions are blurred somewhat for the String and wrapper classes because they have CachedValues. It's generally best to AvoidTheEqualityOperator altogether when dealing with objects.

Winston



Yes Winston. I tried tyhe same operations for String objects. The behavior is not the same because of the String Constant Pool concept.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Yes Winston. I tried tyhe same operations for String objects. The behavior is not the same because of the String Constant Pool concept.


That depends on how you used Strings in your code.
If you were using something like String s = new String("xxxx") in your code, then the behaviour would be the same. If (as I suspect) you were using String s = "xxxx" then the behaviour would be different.

Try replacing
with

and see what result you get. Then change it to


 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Mansukhdeep Thind wrote:Yes Winston. I tried tyhe same operations for String objects. The behavior is not the same because of the String Constant Pool concept.


That depends on how you used Strings in your code.
If you were using something like String s = new String("xxxx") in your code, then the behaviour would be the same. If (as I suspect) you were using String s = "xxxx" then the behaviour would be different.

Try replacing
with

and see what result you get. Then change it to




Yes Joanne, replacing i1 and i2 values by any integer between -128 through 0 to 128 throws both results as true . After 128 though(and below -127) , only i1.equals(i2) throws true. Why do integer values get cached only between this range? Is it OS or JVM specific?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Why do integer values get cached only between this range?


That was the value chosen by the designers. It was probably a trade-off between the usefulness of having certain values cached and the memory requirements for creating a cache. The range is true for all JVMs but the upper limit can be extended (but not the lower one).
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Mansukhdeep Thind wrote:Why do integer values get cached only between this range?


That was the value chosen by the designers. It was probably a trade-off between the usefulness of having certain values cached and the memory requirements for creating a cache. The range is true for all JVMs but the upper limit can be extended (but not the lower one).



OK now.. thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic