Infosys Java and Spring Boot interview questions and answers
Core Java Interview Questions
What is the difference between == and .equals()?
1. == Operator:
- It is a comparison operator.
- It compares memory addresses (i.e., it checks if both references point to the same object in memory).
- It is used mostly for comparing primitive types and object references.
int a = 10;
int b = 10;
System.out.println(a == b); // Output: trueBoth a and b are primitive types and have the same value, so == returns true.
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // Output: falseEven though both strings have the same content, they are stored in different memory locations, so == returns false.
2. .equals() Method:
- It is a method defined in the
Objectclass and overridden in classes likeString. - It is used to compare the contents (values) of two objects.
- It is used mostly with non-primitive types (objects).
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1.equals(s2)); // Output: trueHere, .equals() checks the content of the strings and returns true.
Key Difference Table:
| Aspect | == | .equals() |
|---|---|---|
| Type | Operator | Method |
| Comparison type | Reference (memory address) | Content (value) |
| Used for | Primitives and object references | Objects |
| Returns true if | Same memory location | Same data/content |
Explain Polymorphism in Java.
Polymorphism is one of the four main pillars of Object-Oriented Programming (OOP) in Java (others are Encapsulation, Inheritance, and Abstraction).
The word “Polymorphism” comes from Greek:
- “Poly” means many
- “Morph” means forms
So, Polymorphism means “many forms” — it allows a single interface to represent different underlying forms (data types or behaviors).
Types of Polymorphism in Java
Java supports two main types of polymorphism:
| Type | Also Known As | Description |
|---|---|---|
| Compile-time Polymorphism | Method Overloading | Happens when multiple methods have the same name but different parameters |
| Runtime Polymorphism | Method Overriding | Happens when a subclass provides a specific implementation of a method defined in the parent class |
1. Compile-Time Polymorphism (Method Overloading)
When multiple methods in the same class have the same name but different parameters (type or number).
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Output: 15
System.out.println(calc.add(5, 10, 15)); // Output: 30
}
}2. Runtime Polymorphism (Method Overriding)
When a child class overrides a method from the parent class using the same method signature.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
a.sound(); // Output: Dog barks
}
}Here, although the reference type is Animal, the actual object is Dog, so the overridden method is executed — this is runtime polymorphism.
Summary
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Type | Compile-time polymorphism | Runtime polymorphism |
| Class | Same class | Inheritance (subclass) |
| Method Name | Same | Same |
| Parameters | Different | Same |
| Return Type | Can be same or different | Must be same or covariant |