报告数组元素类型的类中有常量时已知长度为零的数组分配。 由于零长度数组不可变,因此可以重用相同的数组实例来节省内存。
示例:
class Item {
// 可重用的 public 零长度数组常量
public static final Item[] EMPTY_ARRAY = new Item[0];
}
class EmptyNode {
Item[] getChildren() {
// 创建不必要的零长度数组
return new Item[0];
}
}
在应用快速修复后:
class EmptyNode {
Item[] getChildren() {
return Item.EMPTY_ARRAY;
}
}