int的默认值为0,而Integer的默认值为null?是真的吗?
int是java提供的8种原始数据类型之一。
Java为每个原始类型提供了封装类,Integer是java为int提供的封装类。
int的默认值为0,而Integer的默认值为null,即Integer可以区分出未赋值和值为0的区别,int则无法表达出未赋值的情况,
尽信书不如无书,简单事情也要动手操作一下。
View Code
1 public class TestJava { 2 3 public static int i; 4 public static Integer j; 5 6 public static void main(String[] args) { 7 System. out.println(i); 8 System. out.println(j); 9 10 } 11 }
执行结果:
0
null
书上所说是正确的。再看一个例子:
View Code
1 public class TestJava { 2 3 public static void main(String[] args) { 4 int i; 5 Integer j; 6 System. out.println(i); 7 System. out.println(j); 8 9 } 10 11 } 12 13 结果是: 14 编译出错.
结果是编译出错。
原因是没有默认值!
补足:
java的基本数据类型
boolean,char,byte,short,int,long,float,double

浙公网安备 33010602011771号