Computer Applications

Predict the output :

Integer c = 155; 
Integer d = 155;
System.out.println(c == d); 
System.out.println(c.equals(d));

Java

Java Library Classes

8 Likes

Answer

false
true

Working

c == d compares two Integer objects and returns false because when we compare two objects using the operator "==", Java compares their reference i.e., their memory address and not their value. Since these are two different objects stored at two different locations in memory, Java produces result false

c.equals(d) method compares the values of the Integer objects after they are auto-unboxed by the compiler and converted to primitive data types. The function returns true as both the primitive type values are equal.

Answered By

6 Likes


Related Questions