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
Predict the output :
double n1 = Double.parseDouble("8.0"); double n2 = Double.parseDouble("2"); System.out.println(n1 + " " + n2);What important is automatically happening in following code ?
long a = 124235L; Long b = new Long(a); long c = b.longValue(); System.out.println(c);In the following code, identify the statement where autoboxing is taking place :
Integer i = new Integer(10); if (i < 100) System.out.println(i); else System.out.println(i + 10);From the following code, do the following :
(i) find its output.
(ii) find out the statements where autoboxing is taking place.
(iii) find out the statements where auto-unboxing is taking place.
Short age = Short.valueOf("35"); Integer salary = Integer.valueOf("2400"); Float height = Float.valueOf("1.78"); // in meters Double weight = Double.valueof ("72.6"); double b = weight / (height * height); System.out.println(age + "takes home" + salary); System.out.println("bmi : " + b);