Given:
public class Whizlabs {
private String name;
private boolean pass;
public static void main(String[] args) {
Whizlabs wb = new Whizlabs();
System.out.print("name = " + wb.name);
System.out.print(",pass = " + wb.pass);
}
}
What would be the output, if it is executed as a program?
A.
name =, pass =
B.
name = null, pass = null
C.
name = null, pass = false
D.
name = null pass = true
E. Compile error.
題解
Java的類別或是物件欄位會按照不同資料型態自動給定初始值。初始值如下:
String、Object:null
byte、short、int、long、float、double:0
char:\0
boolean:false
byte、short、int、long、float、double:0
char:\0
boolean:false