基本类型包装类 自动装箱拆箱


ValueOf()源码:

  1. package cn.itcast.wrapper;
  2. public class WrapperDemo3 {
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. Integer x = new Integer(3);
  8. Integer y = new Integer("3");
  9. System.out.println(x == y);// false
  10. System.out.println(x.equals(y));// true
  11. System.out.println("--------------------");
  12. Integer a = 127;
  13. // Integer aa = Integer.valueOf(30);
  14. // 通过valueOf源码发现,原来将字节类型的所有数据进行缓存。如果自动装箱的数据都在缓存中。
  15. // 就不会建立新的Integer对象。而是从缓存中取出这个对象。所以a和b如果值是30,指向的对象是同一个。
  16. Integer b = 127;
  17. System.out.println(a == b);
  18. System.out.println(a.equals(b));
  19. }
  20. }






posted @ 2017-06-01 16:43  玄霄2015  阅读(88)  评论(0编辑  收藏  举报