Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between == and equals

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Correct


Thank you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10234
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic