• 7367-xxx-xxx
  • rahul@mastergthoughts.com
  • Sitamarhi, Bihar, India

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: true

    Both 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: false

    Even 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 Object class and overridden in classes like String.
    • 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: true

    Here, .equals() checks the content of the strings and returns true.

    Key Difference Table:

    Aspect==.equals()
    TypeOperatorMethod
    Comparison typeReference (memory address)Content (value)
    Used forPrimitives and object referencesObjects
    Returns true ifSame memory locationSame 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:

    TypeAlso Known AsDescription
    Compile-time PolymorphismMethod OverloadingHappens when multiple methods have the same name but different parameters
    Runtime PolymorphismMethod OverridingHappens 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

    FeatureMethod OverloadingMethod Overriding
    TypeCompile-time polymorphismRuntime polymorphism
    ClassSame classInheritance (subclass)
    Method NameSameSame
    ParametersDifferentSame
    Return TypeCan be same or differentMust be same or covariant