我对下面的代码有些困惑:
class BooksTestDrive {
public static void main(String [] args) {
String [] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
}
}
我假设这会返回NullPointerException
错误,因为没有初始化任何对象。
我以为我需要做
class BooksTestDrive {
public static void main(String [] args) {
String [] islands = new String[4];
islands[0] = new String();
// etc..
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";
}
}
为什么这里没问题?为什么没有抛出异常?