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

Discussion

The program is run with java -ea:com.acme... com.acme.util.Program32 - what will be the output?

package com.acme.util;

public class Program32
{
    public static void main(String[] args)
    {
        String name = null;
        assert name != null : "name missing";
        System.out.println(name.length());
    }
}

  • A.Compilation fails.
  • B.No output is produced.
  • C.A NullPointerException is thrown.
  • D.An AssertionError with message name missing

Answer: D

The three dots cover the named package and everything beneath it, and com.acme.util sits beneath com.acme, so assertions are enabled here. The assertion fails on the null name and throws before the dereference, turning what would have been a NullPointerException into a clear message. Compare the sibling-package case, where the switch named a package that is not an ancestor.

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

Post your comment

Please log in or register to post a comment.