-2

I know that == compares whether two objects are the same and .equals() compares whether the contents inside the objects is the same. How come you can use == on int's and it would compare the contents.

int x=5;
int y=5;

if(x==y)
System.out.print("They are equal");

This code would print the statement.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2871354
  • 530
  • 1
  • 5
  • 15

3 Answers3

4

== does exactly one thing in Java; it compares two values.

If you're talking about primitives, it compares their values.

int x = 1;
int x2 = 1;
if (x == x2) { ... } 

That if would evaluate to true

int x = 1;
int x2 = 2;
if (x == x2) { ... } 

That if condition would evaluate to false

If you're talking about object references, it compares the reference values they contain. This has nothing to do with the contents of the objects being referenced. It compares whether or not the two variables being compared contain the same reference value (i.e. contain the same reference to an object in memory).

MyClass mc1 = new MyClass();
MyClass mc2 = mc1;
if (mc1 == mc2) { .. }

That if condition will evaluate to true

MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
if (mc1 == mc2) { .. }

That if condition will evaluate to false

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
1

You said,

== compares whether two objects are the same

But in case of primitive data types, the values of the object are compared.

It is mentioned in Java Documentation Numerical Equality Operators == and !=

If the promoted type of the operands is int or long, then an integer equality test is performed.

If the promoted type is float or double, then a floating-point equality test is performed.

Your question,

How come you can use == on int's and it would compare the contents.

Just use == only, as on primitives data types, there contents will be compared.

If you need to compare the contents of two object references then use .equals function but do remember to overload the equals function and provide your condition to check equality.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
0

== between two objects, such as strings, compares whether they are the same instance. .equals(), on the other hand, compares them to see if they have the same value.

Consider the possibility that you have a class called Point. Points are made up of two ints, x and y and are allowed to move. I have two points, A, at (0, 0), and B, at (1, 0). Right now, neither == nor .equals() would return true. A moment later, B moves to be at (0, 0). == is still false, because I still have two instances of points and they aren't the same, but .equals() is now true because they have the same value of (0, 0).

Similarly, you should use .equals() if you want to check if two strings have the same value (you almost always want that,) or == if you want to check if two strings are the exact same string.

== behaves differently when comparing primitive numbers, like straight up ints. == is a check to see if they have the same value and there's no equivalent concept of checking for their identity - if you'd like to have ints that have identities, consider using the Integer class instead of the int primitive.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193