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」字符串。
