What will the following code print:

public static void main(String[] args) {
    Integer i = 10;
    Integer x = 10;
    if (x == i) {
        System.out.print("true");
    } else {
        System.out.print("false");
    }
}
Explanation
256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned.

Follow CodeGalaxy

Mobile Beta

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