Please justify the out put : public class WrapTest { public static void main(Str
ID: 3753785 • Letter: P
Question
Please justify the out put :
public class WrapTest {
public static void main(String[] args) {
int result = 0;
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if (x == y) {
result = 1;
if (x.equals(y)) {
result = result + 10;
if (x.equals(z)) {
result = result + 100;
if (x.equals(x2)) {
result = result + 1000;
if (x.equals(z2)) {
result = result + 10000;
System.out.println("result = " + result);
Explanation / Answer
public class WrapTest { public static void main(String[] args) { int result = 0; short s = 42; Long x = new Long("42"); // x here is 42 Long y = new Long(42); // y here is 42 Short z = new Short("42"); // z here is 42 Short x2 = new Short(s); // x2 here is 42 Integer y2 = new Integer("42"); // y2 here is 42 Integer z2 = new Integer(42);// z2 here is 42 // x, y, z, x2, y2, z2 all these are objects(not just data variables) and all of them have a value of 42. // since x and y are two different objects, even though they have same value of 42, they are not pointing to same object in memory. // so, x == y is always false. and hence, control will not go inside the body of if. // so, nothing is printing onto the screen. if (x == y) { result = 1; if (x.equals(y)) { result = result + 10; if (x.equals(z)) { result = result + 100; if (x.equals(x2)) { result = result + 1000; if (x.equals(z2)) { result = result + 10000; System.out.println("result = " + result); } } } } } } }