Output of Java Program | Set 8

Last Updated : 7 Jan, 2026

In this article, we predict the output of Java programs and explain the result based on core Java concepts such as String comparison, method binding, exception handling, and multithreading.

1. String Comparison using ==

This program demonstrates how the == operator compares object references rather than string content in Java.

Java
class GfG {
    public static void main(String args[]) {
        String s1 = new String("geeksforgeeks");
        String s2 = new String("geeksforgeeks");
        if (s1 == s2)
            System.out.println("Equal");
        else
            System.out.println("Not equal");
    }
}

Output
Not equal

Explanation:

  • s1 and s2 refer to different objects in memory.
  • The == operator compares references, not content.
  • Hence, the condition is false and "Not equal" is printed.
  • To compare content, .equals() should be used

2. Static Binding vs Dynamic Binding

This program illustrates the difference between static binding and dynamic binding using inheritance and method overriding.

Java
class Person {
    private void who() {
        System.out.println("Inside private method Person(who)");
    }

    public static void whoAmI() {
        System.out.println("Inside static method, Person(whoAmI)");
    }

    public void whoAreYou() {
        who();
        System.out.println("Inside virtual method, Person(whoAreYou)");
    }
}

class Kid extends Person {
    private void who() {
        System.out.println("Kid(who)");
    }

    public static void whoAmI() {
        System.out.println("Kid(whoAmI)");
    }

    public void whoAreYou() {
        who();
        System.out.println("Kid(whoAreYou)");
    }
}

public class Gfg {
    public static void main(String args[]) {
        Person p = new Kid();
        p.whoAmI();
        p.whoAreYou();
    }
}

Output
Inside static method, Person(whoAmI)
Kid(who)
Kid(whoAreYou)

Explanation:

  • Static methods use compile-time (static) binding, so Person.whoAmI() is called.
  • Instance methods use runtime (dynamic) binding, so Kid.whoAreYou() is called.
  • Private methods are not overridden, they are class-specific.

3. Try–Catch–Finally Execution

This program shows the execution flow of try–catch–finally blocks when no exception occurs.

Java
class GfG {
    public static void main(String args[]) {
        try {
            System.out.println("First statement of try block");
            int num = 45 / 3;
            System.out.println(num);
        }
        catch (Exception e) {
            System.out.println("Gfg caught Exception");
        }
        finally {
            System.out.println("finally block");
        }
        System.out.println("Main method");
    }
}

Output
First statement of try block
15
finally block
Main method

Explanation:

  • No exception occurs, so the catch block is skipped.
  • The finally block always executes.
  • Program continues normally after the try-catch-finally block.

4. Multithreading and Thread Names

This program explains the difference between calling run() and start() in Java multithreading.

Java
class One implements Runnable {
    public void run() {
        System.out.print(Thread.currentThread().getName());
    }
}

class Two implements Runnable {
    public void run() {
        new One().run();
        new Thread(new One(),"gfg2").run();
        new Thread(new One(),"gfg3").start();
    }
}

class Three {
    public static void main (String[] args) {
        new Thread(new Two(),"gfg1").start();
    }
}

Output
gfg1gfg1gfg3

Explanation:

  • run() executes in the current thread, so "gfg1" is printed twice.
  • start() creates a new thread, so "gfg3" is printed separately.
  • Only start() creates a new thread; run() behaves like a normal method call.
     
Comment