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

Discussion

The program is run with java -ea Fragment90 - what will be the output?

import java.util.*;

public class Fragment90
{
    public static void main(String[] args)
    {
        assert Objects.equals(null, null);
        System.out.println(Objects.equals("a", null));
    }
}

  • A.A NullPointerException is thrown.
  • B.The program prints false
  • C.The program prints true
  • D.An AssertionError is thrown.

Answer: B

Objects.equals is null-safe: two nulls are equal, so the assertion passes, and comparing a non-null with null returns false rather than throwing. That is the whole reason the helper exists, since "a".equals(null) is safe but s.equals("a") with a null s is not.

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

Post your comment

Please log in or register to post a comment.