Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line rests on a mistaken assumption?

public class Sample17
{
    public static void main(String[] args)
    {
        String a = "java";
        String b = new String("java");
        assert a == b : "not equal";
        System.out.println("same");
    }
}

  • A.assert a == b : "not equal";
  • B.String b = new String("java");
  • C.System.out.println("same");
  • D.Nothing here is mistaken.

Answer: A

== compares references, and new String is required to produce a distinct object, so the assertion fails even though the text matches. The intent was clearly value equality, which is a.equals(b). The line that creates the second object is legal; only the comparison is wrong.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.