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 给外婆敬酒祝福语简短