What will be the result of compilation and execution of the following code?

Integer i = new Integer("10");
if (i.toString() == i.toString())
   System.out.println("Equal");
else
   System.out.println("Not Equal");
Explanation
Method toString() returns a String representation of an object.
Every time it is called on an object of class Integer, a new String object is created. Operator == compares two references to two String objects, and not the real values of those String objects.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes