Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Comparison Questions - Part 1
JAVA

Comparison Questions - Part 1

Practice 15 Java comparison questions covering comparison operators, ==, equals(), compareTo(), and wrapper versus reference comparison.

1. What does == print for equal int primitives?

int a = 10;
int b = 10;

System.out.println(a == b);

Output: true

For primitives, == compares values, not references. Both hold 10, so the result is true. Primitives have no reference identity to compare.

2. == and != on different int values

int a = 10;
int b = 20;

System.out.println(a == b);
System.out.println(a != b);

Output:

false
true

10 == 20 is false; 10 != 20 is true. Both operators compare primitive values directly.

3. == on two identical String literals ⭐⭐⭐⭐⭐

String a = "Java";
String b = "Java";

System.out.println(a == b);

Output: true

Both literals are compile-time constants that resolve to the same object in the string pool, so == (reference comparison) is true. This is the foundational string-pool behavior.

4. == on a literal vs a new String ⭐⭐⭐⭐⭐

String a = "Java";
String b = new String("Java");

System.out.println(a == b);

Output: false

new String("Java") creates a distinct heap object, even though it holds the same content. == compares references, and the two references point to different objects — so it is false, regardless of equal content.

5. .equals() on equal-content Strings ⭐⭐⭐⭐⭐

String a = "Java";
String b = new String("Java");

System.out.println(a.equals(b));

Output: true

String.equals() compares content, not references. Both contain "Java", so the result is true even though they are different objects.

6. Three-way String comparison ⭐⭐⭐⭐⭐

String a = "Java";
String b = "Java";
String c = new String("Java");

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));

Output:

true
false
true

a and b share the pooled literal object → true. c is a separate new object → false for ==. Content is equal → true for .equals(). This one question bundles the entire == vs .equals() lesson.

7. compareTo() on equal Strings

String a = "Java";
String b = "Java";

System.out.println(a.compareTo(b));

Output: 0

compareTo() returns 0 when the two strings are equal, a negative value when a is lexicographically smaller, and a positive value when a is larger.

8. compareTo() — smaller first string

String a = "Apple";
String b = "Banana";

System.out.println(a.compareTo(b));

Output: -1

"Apple" precedes "Banana" lexicographically. compareTo() returns the difference of the first differing characters: 'A'(65) - 'B'(66) = -1. The sign matters more than the magnitude.

9. compareTo() — larger first string

String a = "Banana";
String b = "Apple";

System.out.println(a.compareTo(b));

Output: 1

"Banana" comes after "Apple". 'B'(66) - 'A'(65) = 1. Again, only the sign (and here the magnitude) tells you the ordering.

10. compareTo() when one string is a prefix

String a = "Java";
String b = "JavaScript";

System.out.println(a.compareTo(b));

Output: -6

"Java" is a prefix of "JavaScript". When no differing character is found, compareTo() returns the length difference: 4 - 10 = -6. So a shorter prefix is lexicographically smaller.

11. equals() vs equalsIgnoreCase() ⭐⭐⭐⭐

String a = "JAVA";
String b = "java";

System.out.println(a.equals(b));
System.out.println(a.equalsIgnoreCase(b));

Output:

false
true

equals() is case-sensitive, so "JAVA""java". equalsIgnoreCase() ignores case, so they are considered equal.

12. == on equal char values

char a = 'A';
char b = 'A';

System.out.println(a == b);

Output: true

char is a primitive. == compares the numeric (Unicode) values, and both are 'A' (65), so the result is true.

13. Relational operators on char values

char a = 'A';
char b = 'B';

System.out.println(a < b);
System.out.println(a > b);

Output:

true
false

Chars compare by their numeric values: 'A' is 65, 'B' is 66. So 65 < 66 is true and 65 > 66 is false.

14. Autoboxed Integer inside the cache range ⭐⭐⭐⭐⭐

Integer a = 100;
Integer b = 100;

System.out.println(a == b);

Output: true

Autoboxing reuses cached Integer objects for values in -128 to 127. Both a and b reference the same cached object, so == is true.

15. Autoboxed Integer outside the cache range ⭐⭐⭐⭐⭐

Integer a = 200;
Integer b = 200;

System.out.println(a == b);

Output: false

200 is outside the cached -128..127 range, so each autoboxing creates a separate object. == compares references → false. Never use == to compare wrapper values; use .equals().

My Private Notes

Notes are auto-saved locally to this device.