zoukankan      html  css  js  c++  java
  • Java中Integer和int的异同

     1 public void Test1() {
     2         int a = 128;
     3         Integer b = 128;
     4         Integer c = 128;
     5         //Integer会自动拆箱成int,所以为ture
     6         System.out.println(a==b);
     7         System.out.println(a==c);
     8     }
     9     
    10     @Test
    11     public void Test2() {
    12         Integer a = 127;
    13         Integer b = 127;
    14         System.out.println(a == b);//此处结果为true,应该没什么疑问Java在编译 Integer c时候编译成 Integer.valueOf(127)
    15         
    16         Integer c = 128;
    17         Integer d = 128;
    18         System.out.println(c == d);//此处结果为false,
    19         /*Integer.valueOf()方法基于减少对象创建次数和节省内存的考虑,缓存了[-128,127]之间的数字。
    20         此数字范围内传参则直接返回缓存中的对象。在此之外,直接new出来。*///源码如下
    21         /* public static Integer valueOf(int i) {
    22                 assert IntegerCache.high >= 127;
    23                 if (i >= IntegerCache.low && i <= IntegerCache.high)
    24                     return IntegerCache.cache[i + (-IntegerCache.low)];
    25                 return new Integer(i);
    26             }*/
    27     }
  • 相关阅读:
    初识计算机
    前端html css
    mysql高级
    mysql多表查询
    mysql数据库查询
    mysql表关系
    mysql数据类型
    mysql数据库介绍
    异步回调 协程
    GIL-全局解释器锁
  • 原文地址:https://www.cnblogs.com/liujie-/p/4718273.html
Copyright © 2011-2022 走看看