Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { String a = "java"; String b =…

914Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        String a = "java";
        String b = new String("java");
        String c = "ja" + "va";
        System.out.println((a == b) + " " + (a == c) + " " + a.equals(b));
    }
}
  • Atrue false true
  • Btrue true true
  • Cfalse false true
  • Dfalse true true
Correct answer: D

Explanation

new String() creates a separate object so a == b is false; "ja" + "va" is a compile-time constant interned with "java" so a == c is true; equals() compares content.

Discussion 0

Nobody has replied yet. Start the discussion!