为什么IntelliJ IDEA会为Exception提供建议?

时间:2017-06-15 18:48:08

标签: java exception intellij-idea exception-handling nullpointerexception

public static List<String> arithmeticProblemToList(**String arithmeticProblem**, boolean resultIsRight,
                                                   double rightResult,
                                                   List<String> previousArithmeticProblemsList) throws Exception {

    Check.checkNotNull(arithmeticProblem);
    Check.checkNotNull(resultIsRight);

    if (resultIsRight)
        previousArithmeticProblemsList.add(arithmeticProblem + "\t\tRICHTIG");
    else {
        previousArithmeticProblemsList.add(arithmeticProblem + "\t\tFALSCH " + rightResult + " wäre richtig");
    }

    return previousArithmeticProblemsList;
}

public static <T> void checkNotNull(T object) throws IllegalArgumentException {

    if (object == null) {
        throw new IllegalArgumentException("Der Parameter darf nicht null sein.");
    }
}

为什么IntelliJ给我一个建议,即如果字符串arithmeticProblem为空,我将得到一个异常,例如,如果resultIsRight没有建议。

1 个答案:

答案 0 :(得分:2)

在Java中,有两种不同类型的数据类型:Primitives和References。

布尔值是基元,不能以任何方式为空。无论是真还是假,但永远不会为空。

字符串是引用类型,引用可以为null。

在此处阅读有关基元的更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html