Java中的数字和非数字的原始数据类型的默认值
在Java中创建实例变量时,您需要对其进行初始化,否则编译器将代表您使用默认值进行初始化-
byte:0
short:0
int:0
long:0
float:0.0
double:0.0
boolean:false
string:null
示例
在下面的Java程序中,将打印Java中数字和非数字基元变量的默认值。
public class DefaultValues {
byte byteVariable;
short shortVariable;
int intVariable;
long longVaraible;
float floatVariable;
double doubleVariable;
boolean boolVariable;
String stringVariable;
public static void main(String args[]){
DefaultValues obj = new DefaultValues();
System.out.println("Java中数值变量的默认值:");
System.out.println("byte: "+obj.byteVariable);
System.out.println("short: "+obj.shortVariable);
System.out.println("int: "+obj.intVariable);
System.out.println("long: "+obj.longVaraible);
System.out.println("float: "+obj.floatVariable);
System.out.println("double: "+obj.doubleVariable);
System.out.println("Java中非数值变量的默认值:");
System.out.println("boolean: "+obj.boolVariable);
System.out.println("string: "+obj.stringVariable);
}
}输出结果
Java中数值变量的默认值: byte: 0 short: 0 int: 0 long: 0 float: 0.0 double: 0.0 Java中非数值变量的默认值: boolean: false string: null
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短