java的Integer缓冲

java.lang.Integer.valueOf(int)方法默认情况下如果参数在-128到127之间,则返回缓存中的对象,否则返回new Integer(int)。

缓存中的对象是JVM第一次使用valueOf方法时初始化的。

可以设置系统属性 java.lang.Integer.IntegerCache.high 修改缓冲区上限,默认为127。参数内容应为大于127的十进制数形式的字符串,否则将被忽略。取值范围为127-Long.MAX_VALUE,但是用时将强转为int。

当系统中大量使用Integer时,增大缓存上限可以节省小量内存。

缓冲区上限将影响某些业务:

      Integer i1 = 127;
      Integer i2 = 127;
      if (i1 == i2)
           System.out.println(true);
      else
           System.out.println(false);

      i1 = 128;
      i2 = 128;
      if (i1 == i2)
           System.out.println(true);
      else
           System.out.println(false);

  以上代码默认情况下将输出:

     true
     false

  当修改缓冲上限大于128时将变为:

  

  true
  true
时间: 2024-08-05 22:08:06

java的Integer缓冲的相关文章

关于Integer缓冲

默认的时候,-128--127范围内的数会被缓冲 但是jvm启动的时候可以修改启动参数-Djava.lang.Integer.IntegerCache.high=2000 来修改上限,但是下限是不可以修改的. public static void main(String[] args) {    Integer in3 =1200;    Integer in2 = 1200;    System.out.println(in3==in2);    Properties p = new Prop

【转载】C#之int与Java之Integer的区别

本文涉及到一些JVM原理和Java的字节码指令,推荐感兴趣的读者阅读一本有关JVM的经典书籍<深入Java虚拟机(第2版)>,将它与我在<.NET 4.0面向对象编程漫谈>中介绍的CLR原理与IL汇编指令作个对比,相信读者会有一定的启发.而仔细对比两个类似事物的异同,是很有效的学习方法之一. 1 奇特的程序输出 前段时间,一个学生给我看了一段“非常诡异”的Java代码:   public class testInteger {    public static void main(

java.lang.Integer.toHexString(b[n] &amp; 0XFF)中0XFF使用的必要性

byte[] b = {1,2,20,108}; String stmp = java.lang.Integer.toHexString(b[n] & 0XFF) 在32位的电脑中数字都是以32格式存放的,如果是一个byte(8位)类型的数字,他的高24位里面都是随机数字,低8位 才是实际的数据.java.lang.Integer.toHexString() 方法的参数是int(32位)类型,如果输入一个byte(8位)类型的数字,这个 方法会把这个数字的高24为也看作有效位,这就必然导致错误,

java中Integer i1= 0; Integer i2= 0; 为什么可以用 == 符号呢

java中Integer i1= 0; Integer i2= 0;  有i1 == i2,且有i1 = 1;那么i2为什么不会变成1呢? 我这个时候的想法是:这是因为java中整型常量是基于int的,而java中有自动包装机制,也就是说看起来i1,i2是Integer类型,但是其底层还是基于int类型的(不能理解的话在java中还有一个地方那就是Arrays.asList(T...args) 他返回一个基于数组的List,如果在List中改变其大小,那么将会引发异常[UnsupportedOp

MySQL分段统计SQL写法 与 Mybatis 报错:java.math.BigDecimal cannot be cast to java.lang.Integer

mysql> select -> sum(case when score<60 then 1 else 0 end) as '<60', -> sum(case when score>=60 and score<=69 then 1 else 0 end) as '60~69', -> sum(case when score>=70 and score<=79 then 1 else 0 end) as '70~79', -> sum(ca

java.lang.Integer can not be cast to java.lang.Long

hibernate 查询出来的结果 id 是int类型,xml文件配置的是int, 在jython 中调用hibernate 进行查询,字段的类型是java.lang.Long, 直接将id 传进去,报java.lang.Integer can not be cast to java.lang.Long 异常,将 id 进行强转,(Long)id,传进去,正常了. java.lang.Integer can not be cast to java.lang.Long,布布扣,bubuko.com

MyBatis出错Result Maps collection does not contain value for java.lang.Integer

Servlet.service() for servlet [SpringMVC] in context with path [/eyou] threw exception [Request processing failed; nested exception is org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.Integer] with root causej

MyBatis查询传一个参数时报错:There is no getter for property named &#39;sleevetype&#39; in &#39;class java.lang.Integer

用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class java.lang.Integer 原因在于测试条件写法有误, <if test="sleevetype==0"><!-- 专属 --> exclusive=1 </if> <if test="sleevetype!=0">&l

Java中Integer类的方法

字段摘要 static int MAX_VALUE              保持 int 类型的最大值的常量可取的值为 231-1. static int MIN_VALUE              保持 int 类型的最小值的常量可取的值为 -231. static int SIZE              以二进制补码形式表示 int 值的位数. static Class<Integer> TYPE              表示基本类型 int 的 Class 实例. 构造方法摘要