What will be the output of the following Java code -
1 2 3 4 |
|
Details and Assumptions
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Integer is not a primitive type and it shouldn't be compared using
==
. The==
comparison only works when the references of both objects are same.Java caches the values of integers from -128 to 127. Hence
a == b
have the same reference due to Java's mechanisms. So the answer returned istrue
.However in case of 300 (which is outside the range of -128 to 127), the values are not cached. Hence two objects having the same value may* have different references. Hence
c == d
returns false.must be used to compare two objects.
Note: two objects outside the range of -128 to 127 may have the same reference. However the probability is low.