In Part 1, we learned that:
-
==and!=work with all primitive data types. - For primitive values,
==compares actual values. - For objects,
==compares references (memory addresses). - Object reference types must be compatible for
==to compile.
In this part, we'll explore some of the most frequently asked Java interview topics, including null, == vs .equals(), the String Pool, Integer Cache, and best practices.
Rule 4: Comparing with null
The null literal represents a reference that points to no object.
Example 1: Reference Points to an Object
String developer = new String("Rajesh");
System.out.println(developer == null);
Output
false
Since developer refers to an actual object, it is not null.
Example 2: Reference Is null
String developer = null;
System.out.println(developer == null);
Output
true
Here, the reference doesn't point to any object.
Example 3: Comparing null with null
System.out.println(null == null);
Output
true
Both operands represent the same null reference.
Important Rule
- If a reference points to an object,
reference == nullreturnsfalse. - If the reference itself is
null,reference == nullreturnstrue.
Rule 5: == vs .equals() (Most Asked Java Interview Question)
This is one of the most common interview questions.
Many beginners assume that == compares object contents.
It doesn't.
Example
String firstName = new String("Rajesh");
String secondName = new String("Rajesh");
System.out.println(firstName == secondName);
System.out.println(firstName.equals(secondName));
Output
false
true
Why?
Two different objects are created.
firstName ───► "Rajesh"
secondName ─► "Rajesh"
The text is the same, but the objects are different.
Therefore,
firstName == secondName
returns
false
However,
firstName.equals(secondName)
compares the contents and returns
true
The Difference Between == and .equals()
== Operator |
.equals() Method |
|---|---|
| Operator | Method |
| Works with primitives and objects | Works only with objects |
| Compares values for primitives | Not applicable for primitives |
| Compares references for objects | Usually compares object contents |
| Cannot be overridden | Can be overridden |
| Faster | Depends on implementation |
String Pool Interview Question
Another very popular interview question.
String first = "Rajesh";
String second = "Rajesh";
String third = new String("Rajesh");
System.out.println(first == second);
System.out.println(first == third);
System.out.println(first.equals(third));
Output
true
false
true
Why?
String literals are stored in the String Pool.
first ──┐
│
second ──┘──► "Rajesh" (String Pool)
third ─────► New String Object
-
firstandsecondreference the same pooled object. -
thirdreferences a new object created usingnew.
Integer Cache Interview Question
Wrapper classes also have caching behavior.
Integer a = 100;
Integer b = 100;
System.out.println(a == b);
Output
true
Now look at this.
Integer x = 200;
Integer y = 200;
System.out.println(x == y);
System.out.println(x.equals(y));
Output
false
true
Why?
Java caches wrapper objects for values between -128 and 127.
Values outside this range create separate objects.
Incompatible Types
Consider the following code.
String developer = new String("Rajesh");
StringBuffer buffer = new StringBuffer("Rajesh");
System.out.println(developer == buffer);
Compile-time error
incomparable types:
String and StringBuffer
Why?
String and StringBuffer are unrelated classes.
Java does not allow reference comparison between unrelated types.
But .equals() Works
String developer = new String("Rajesh");
StringBuffer buffer = new StringBuffer("Rajesh");
System.out.println(developer.equals(buffer));
Output
false
No compile-time error occurs.
The equals() method simply returns false.
Why Does String.equals() Behave Differently?
The String class overrides the equals() method to compare characters.
String first = new String("Rajesh");
String second = new String("Rajesh");
System.out.println(first.equals(second));
Output
true
Now look at StringBuffer.
StringBuffer first = new StringBuffer("Rajesh");
StringBuffer second = new StringBuffer("Rajesh");
System.out.println(first.equals(second));
Output
false
StringBuffer does not override equals().
Therefore, it inherits the default implementation from Object, which performs reference comparison.
Classes That Override equals()
Many commonly used Java classes override equals() for content comparison.
Examples include:
String- Wrapper classes (
Integer,Long,Double, etc.) - Most collection classes (
ArrayList,HashSet,HashMap, etc.)
Safely Comparing with null
This code is safe.
String developer = null;
System.out.println(developer == null);
Output
true
However,
String developer = null;
System.out.println(developer.equals("Rajesh"));
throws
NullPointerException
A safer approach is
"Rajesh".equals(developer)
or
Objects.equals(developer, "Rajesh")
The Objects.equals() method safely handles null values.
Arrays and Equality
Another popular interview question.
int[] numbers1 = {1, 2, 3};
int[] numbers2 = {1, 2, 3};
System.out.println(numbers1 == numbers2);
System.out.println(numbers1.equals(numbers2));
Output
false
false
Neither compares array contents.
The correct way is
Arrays.equals(numbers1, numbers2);
Output
true
Relationship Between == and .equals()
| Given | Conclusion |
|---|---|
r1 == r2 is true
|
r1.equals(r2) is always true
|
r1 == r2 is false
|
.equals() may be true or false
|
r1.equals(r2) is true
|
== may be true or false
|
r1.equals(r2) is false
|
== is always false
|
Common Beginner Mistakes
Using == to Compare Strings
Incorrect
String first = new String("Rajesh");
String second = new String("Rajesh");
System.out.println(first == second);
Use
first.equals(second)
Calling .equals() on a null Reference
Incorrect
developer.equals("Rajesh");
Correct
"Rajesh".equals(developer)
or
Objects.equals(developer, "Rajesh")
Assuming Wrapper Objects Behave Like Primitives
Remember that wrapper objects are objects.
== compares references.
.equals() compares values.
Best Practices
- Use
==for comparing primitive values. - Use
==only when checking whether two object references point to the same object. - Use
.equals()when comparing object contents. - Use
Objects.equals()when a reference may benull. - Use
Arrays.equals()to compare array contents. - Avoid using
==for comparing strings unless you intentionally want to compare references.
Quick Memory Trick 🧠
==checks whether it's the same object..equals()checks whether the contents are the same.
Think of two books:
- Same physical book →
== - Different books with identical text →
.equals()
Key Takeaways
- Equality operators work with all primitive data types, including
boolean. - For primitives,
==compares values. - For objects,
==compares references. -
.equals()is intended for content comparison and can be overridden. -
Stringoverridesequals(), butStringBufferdoes not. - String literals are stored in the String Pool.
- Wrapper classes cache values from -128 to 127.
- Use
Objects.equals()for null-safe comparisons. - Use
Arrays.equals()to compare array contents.
Happy Coding!
Top comments (0)