Question 239 / 297:  What is the output of compiling and running the following code?
class Test {

public static void main(String arg[]) {

Integer x = 1000;

long y = 1000;

Integer z = 1000;

System.out.println(x==y);

System.out.println(x==z);

}

}

A  true then false will be printed
B  true then true will be printed
C  false then true wil be printed
D  false then false will be printed
E  compilation error at the first print statement
F  compilation error at the second print statement
<< First < Previous Next > Last >>
Explanation:

The "==" and "!=" operators expect operands of numeric, char types or references to compare between.

In numeric or char types comparison, the values of operands are compared, whereas references comparison checks if the two references are pointing to the same object

When "==" or "!=" is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive like in the first print statement.

In the second statement, a reference comparison is used, and since "x" and "z" are referencing different objects the output is "false"

Note: When comparing two wrappers of type :

1. Boolean

2. or Byte

3. or Short or Integer from -128 to 127 ,

4. or Character from \u0000 to \u007f (7f is 127 in decimal)

a value comparison not a reference comparison is used.

Exam Home Page
Ask
Briana Knowlton
Start Quiz
Prateek Ashtikar
Start Quiz
Candice Butts
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>