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 教师闺蜜祝福语简短