题二:Integer声明相关问题

//@题2
System.out.println("100与100的比较");
int a = 100;
int b = 100;
Integer c = 100;
Integer d = 100;
Integer e = new Integer(100);
System.out.println(a == b); //true
System.out.println(c == d); //true
System.out.println(a == c); //true
System.out.println(a == e); //true
System.out.println(c == e); //false
System.out.println(a == c.intValue()); //true
System.out.println(a == e.intValue()); //true
System.out.println(c == e.intValue()); //true
System.out.println(c.intValue() == e); //true
System.out.println(c.intValue() == d.intValue());//true
System.out.println(c.intValue() == e.intValue());//true
System.out.println();
System.out.println("150与150的比较");
int f = 150;
int g = 150;
Integer h = 150;
Integer i = 150;
Integer j = new Integer(150);
System.out.println(f == g); //true
System.out.println(h == i); //false
System.out.println(f == h); //true
System.out.println(f == j); //true
System.out.println(h == j); //false
System.out.println(f == h.intValue()); //true
System.out.println(f == j.intValue()); //true
System.out.println(h == i.intValue()); //true
System.out.println(h.intValue() == i); //true
System.out.println(h.intValue() == i.intValue()); //true
System.out.println(h.intValue() == j.intValue()); //true
System.out.println();
System.out.println("250与250的比较");
int k = 250;
Integer l = 250;
Integer m = 250;
Integer n = new Integer(250);
System.out.println(k == l); //true
System.out.println(k == n); //true
System.out.println(l == m); //false
System.out.println(l == n); //false
System.out.println(k == l.intValue()); //true
System.out.println(k == n.intValue()); //true
System.out.println(l.intValue() == n); //true
System.out.println(l == n.intValue()); //true
System.out.println(l.intValue() == n.intValue()); //true
System.out.println();
System.out.println("100+150与250的(int + any == int)比较");
System.out.println(a + f == k); //true
System.out.println(a + h == k); //true
System.out.println(a + h.intValue() == k); //true
System.out.println(a + j == k); //true
System.out.println(a + j.intValue() == k); //true
System.out.println("100+150与250的(Integer + Integer == int)比较");
System.out.println(c + h == k); //true
System.out.println(c + h.intValue() == k); //true
System.out.println(c.intValue() + h == k); //true
System.out.println(c.intValue() + h.intValue() == k); //true
System.out.println("100+150与250的(Integer + new Integer == int)比较");
System.out.println(c + j == k); //true
System.out.println(c + j.intValue() == k); //true
System.out.println(c.intValue() + j == k); //true
System.out.println(c.intValue() + j.intValue() == k); //true
System.out.println("100+150与250的(new Integer + new Integer == int)比较");
System.out.println(e + j == k); //true
System.out.println(e.intValue() + j == k); //true
System.out.println(e + j.intValue() == k); //true
System.out.println(e.intValue() + j.intValue() == k); //true
System.out.println();
System.out.println("100+150与250的(int + any == Integer)比较");
System.out.println(a + f == l); //true
System.out.println(a + h == l); //true
System.out.println(a + h.intValue() == l); //true
System.out.println(a + j == l); //true
System.out.println(a + j.intValue() == l); //true
System.out.println("100+150与250的(Integer + Integer == Integer)比较");
System.out.println(c + h == l); //true
System.out.println(c + h == l.intValue()); //true
System.out.println(c + h.intValue() == l); //true
System.out.println(c + h.intValue() == l.intValue()); //true
System.out.println(c.intValue() + h == l); //true
System.out.println(c.intValue() + h == l.intValue()); //true
System.out.println(c.intValue() + h.intValue() == l); //true
System.out.println(c.intValue() + h.intValue() == l.intValue()); //true
System.out.println("100+150与250的(Integer + new Integer == Integer)比较");
System.out.println(c + j == l); //true
System.out.println(c + j == l.intValue()); //true
System.out.println(c + j.intValue() == l); //true
System.out.println(c + j.intValue() == l.intValue()); //true
System.out.println(c.intValue() + j == l); //true
System.out.println(c.intValue() + j == l.intValue()); //true
System.out.println(c.intValue() + j.intValue() == l); //true
System.out.println(c.intValue() + j.intValue() == l.intValue()); //true
System.out.println("100+150与250的(new Integer + new Integer == Integer)比较");
System.out.println(e + j == l); //true
System.out.println(e + j == l.intValue()); //true
System.out.println(e + j.intValue() == l); //true
System.out.println(e + j.intValue() == l.intValue()); //true
System.out.println(e.intValue() + j == l); //true
System.out.println(e.intValue() + j == l.intValue()); //true
System.out.println(e.intValue() + j.intValue() == l); //true
System.out.println(e.intValue() + j.intValue() == l.intValue()); //true
System.out.println();
System.out.println("100+150与250的(int + any == new Integer)比较");
System.out.println(a + f == n); //true
System.out.println(a + h == n); //true
System.out.println(a + h.intValue() == n); //true
System.out.println(a + j == n); //true
System.out.println(a + j.intValue() == n); //true
System.out.println("100+150与250的(Integer + Integer == new Integer)比较");
System.out.println(c + h == n); //true
System.out.println(c + h == n.intValue()); //true
System.out.println(c + h.intValue() == n); //true
System.out.println(c + h.intValue() == n.intValue()); //true
System.out.println(c.intValue() + h == n); //true
System.out.println(c.intValue() + h == n.intValue()); //true
System.out.println(c.intValue() + h.intValue() == n); //true
System.out.println(c.intValue() + h.intValue() == n.intValue()); //true
System.out.println("100+150与250的(Integer + new Integer == new Integer)比较");
System.out.println(c + j == n); //true
System.out.println(c + j == n.intValue()); //true
System.out.println(c + j.intValue() == n); //true
System.out.println(c + j.intValue() == n.intValue()); //true
System.out.println(c.intValue() + j == n); //true
System.out.println(c.intValue() + j == n.intValue()); //true
System.out.println(c.intValue() + j.intValue() == n); //true
System.out.println(c.intValue() + j.intValue() == n.intValue()); //true
System.out.println("100+150与250的(new Integer + new Integer == new Integer)比较");
System.out.println(e + j == n); //true
System.out.println(e + j == n.intValue()); //true
System.out.println(e + j.intValue() == n); //true
System.out.println(e + j.intValue() == n.intValue()); //true
System.out.println(e.intValue() + j == n); //true
System.out.println(e.intValue() + j == n.intValue()); //true
System.out.println(e.intValue() + j.intValue() == n); //true
System.out.println(e.intValue() + j.intValue() == n.intValue()); //true

① Integer在JVM中的缓存范围为-128~127,超过范围会调用Integer.valueOf()方法进行装箱。

② 进行计算时,Integer会通过intValue()进行拆箱,转换为int值再进行计算。

③ Integer.valueOf()进行装箱操作,Integer.intValue()进行拆箱操作。

④ Integer为int的封装类,int为Java的基本类型。

⑤ Integer的默认值为null,int的默认值为0。

⑥ Integer需要实例化,int不需要。

⑦ Integer是引用对象,需要一个引用指向Integer的对象,int是基本类型,可以直接存储数值。

⑧ -128~127的范围内,Integer的值放在栈中,超过这个范围最终会通过new Integer(int)进行装箱,从而存储在堆中,与直接声明new Integer(int)同样存储在堆中。int参数的值始终在栈中,new Integer(int)存在堆中。与new String("abc")相似,abc在常量池中,而new String("abc") 在堆中。

参考链接:

Java-Integer与Int类型的比较-装箱与拆箱详解https://blog.csdn.net/guozhiwang_0311/article/details/79745789

posted @ 2020-01-09 16:49  bobwuming  阅读(178)  评论(0)    收藏  举报