自己做的网站显示不安全怎么回事,电商沙盘seo裤子关键词,商城网站建设多少钱,免费ps模板下载网站数据类型之Integer与int Java入门 基本数据类型 众所周知#xff0c;Java是面向对象的语言#xff0c;一切皆对象。但是为了兼容人类根深蒂固的数据处理习惯#xff0c;加快常规数据的处理速度#xff0c;提供了9种基本数据类型#xff0c;他们都不具备对象的特性#xf… 数据类型之Integer与int Java入门 基本数据类型 众所周知Java是面向对象的语言一切皆对象。但是为了兼容人类根深蒂固的数据处理习惯加快常规数据的处理速度提供了9种基本数据类型他们都不具备对象的特性没有属性和行为。Java的9种基本数据类型包括 boolean、byte、char、short、int、long、double和refvar。前8种数据类型表示生活中的真假、字符、整数和小数最后一种refvar是面向对象世界中的引用变量也叫引用句柄。此处认为它也是一种基本数据类型。 前8种都有其对应的包装数据类型除了char的对应包装类名为 Character int为 Integer外其它所有对应的包装类名就是把首字母大写即可。下面列出了这8种基本数据类型的空间占用大小(bit)及对应的包装类等信息。 boolean/1/Booleanbyte/8/Bytechar/16/Charactershort/16/Shortint/32/Integerfloat/32/Floatlong/64/Longdouble/64/Double 包装类型 前8种基本数据类型都有相应的包装类因为Java的设计理念是一切皆是对象在很多情况下需要以对象的形式操作比如hashCode()获取哈希值或者getClass获取类等。包装类的存在解决了基本数据类型无法做到的事情泛型类型参数、序列化、类型转换、高频区间数据缓存。 重点要说的就是最后一项缓存池 缓存池 事实上除了Float和Double外其他包装类型都会缓存。接下来我以介绍Integer的方式来讲解缓存。 先看一段代码 new Integer(123);
Integer.valueOf(123);new Integer(123) 与 Integer.valueOf(123) 的区别在于 new Integer(123) 每次都会新建一个对象Integer.valueOf(123) 会使用缓存池中的对象多次调用会取得同一个对象的引用。valueOf() 方法的源码。 public static Integer valueOf(int i) {if (i IntegerCache.low i IntegerCache.high)return IntegerCache.cache[i (-IntegerCache.low)];return new Integer(i);
}如上源代码赋值数据i在缓存区间内直接返回缓存池中的Integer对象否则就会new一个对象。 因此比较时会出现如下问题 public class TestIntegerCache {public static void main(String[] args) {Integer i1 100;Integer i2 100;System.out.println(i1 i2);//trueInteger i3 500;Integer i4 500;System.out.println(i3 i4);//falseSystem.out.println(i3.equals(i4));//true}
} 所以推荐所有包装类型对象之间的比较全部使用equals方法。 在 Java 8 中Integer 缓存池的大小默认为 -128~127。 static final int low -128;
static final int high;
static final Integer cache[];static {// high value may be configured by propertyint h 127;String integerCacheHighPropValue sun.misc.VM.getSavedProperty(java.lang.Integer.IntegerCache.high);if (integerCacheHighPropValue ! null) {try {int i parseInt(integerCacheHighPropValue);i Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.}}high h;cache new Integer[(high - low) 1];int j low;for(int k 0; k cache.length; k)cache[k] new Integer(j);// range [-128, 127] must be interned (JLS7 5.1.7)assert IntegerCache.high 127;
}基本类型对应的缓冲池如下 boolean values true and falseall byte valuesshort values between -128 and 127int values between -128 and 127char in the range \u0000 to \u007F相关参考StackOverflow : Differences between new Integer(123), Integer.valueOf(123) and just 123 转载于:https://www.cnblogs.com/jiaweixie/p/9931226.html