It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Difference between == and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between == and equals" Watch "Difference between == and equals" New topic
Author

Difference between == and equals

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Correct


Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Joanne Neal wrote:Correct


Thank you.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

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

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Aug 05, 2005
Posts: 3429
    
  12
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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Aug 05, 2005
Posts: 3429
    
  12
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

Joined: Jul 27, 2010
Posts: 1157

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between == and equals
 
Similar Threads
Immutable wrapper classes
difference between add("hello") and add(new String("hello"));
Equals operator
== and equals()
How many Objects are garbage collected?