Challenge Statement
Predict the Output with proper reasons
Problem Statement 1:
Problem Statement 2:
Right Answer
Problem Statement 1: Save Mother Earth to save ourselvesProblem Statement 2: true
false
Reasoning
Problem Statement 1:
The static method is not a member of any object, rather it is a member of a class itself. Hence when b1, which indeed is a null object , calls the static method, the static method gets called only with the type of the object, not with the value, ensuring that the static method gets called in a static way.
If you decompile the .class file of TestBox class, you will see that instead of newBox, the print method is being called as Box.print();
Problem Statement 2:
When an Integer object is created through autoboxing or Integer.valueOf (n) method, it pools -128 to 127 (both inclusive) in a cache. Hence multiple objects having values within this range will be served from a cache. So first and second these two objects have same reference in cache, hence we are getting the output as true. But any values outside this range will never be cached, so two different objects third and fourth, will have two different referenced. So their equality will never succeed.
Right Answer Given By
Meet Mr. Jagannath Kundu |
No comments:
New comments are not allowed.