Given:
public class Painting {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public static void main(String[] args) {
Painting obj1 = new Painting();
Painting obj2 = new Painting();
obj1.setType(null);
obj2.setType("Fresco");
System.out.print(obj1.getType() + " : " + obj2.getType());
}
}
What is the result?
A.
: Fresco
B.
null : Fresco
C.
Fresco : Fresco
D. A NullPointerException is thrown at runtime
題解
使用「+」運算子時,若其中至少一個運算元為字串(物件)型態,則為字串連接運算。若連接的字串(物件)參考是null的話,則會被當作長度為4的「null」字串。