示例:
if (array.length < index) {
System.out.println(array[index]);
} // 数组索引总是超出界限
if (str == null) System.out.println("str is null");
System.out.println(str.trim());
// 最后一条语句可能会抛出 NPE
@NotNull
Integer square(@Nullable Integer input) {
// 违反了方法约定
return input == null ? null : input * input;
}
检查行为可能由许多注解控制,例如 nullability 注解、@Contract
注解、@Range
注解等。
配置检查:
@Nullable
注解。 您还可以使用配置注解按钮配置可 null 性注解。null
文字的方法参数。@Nullable
但始终返回非 null 值的方法。 在这种情况下,建议您将注解更改为 @NotNull
。if (alwaysFalseCondition) throw new IllegalArgumentException();
等条件。assert
语句。 默认情况下,该选项被禁用,这意味断言被视为正在执行(-ea 模式)。 如果启用该选项,断言将被完全忽略(-da 模式)。true
、false
或 null
)的变量时添加警告。