Question discussion

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

1576Medium 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 = "hi", b = new String("hi");
        System.out.println((a == b) + " " + a.equals(b) + " " + (a == b.intern()));
    }
}
  • Atrue true true
  • Bfalse false true
  • Cfalse true true
  • Dfalse true false
Correct answer: C

Explanation

a and b are different objects (== is false), their contents are equal (equals is true), and intern() returns the pooled literal, so a == b.intern() is true.

Discussion 0

Nobody has replied yet. Start the discussion!