学习实战全笔记--JavaSE--包装类的特性--用法示例(JDK8)
2015-04-11 11:07
639 查看
我们知道Java是面向对象的语言,但是为了照顾程序员对以前编程语言的习惯,增加了八种基本数据类型(他们不是对象)如下:
<1> 分类与初值:
(六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型)
1、整数:包括int,short,byte,long ,初始值为0
2、浮点型:float,double ,初始值为0.0
3、字符:char ,初始值为空格,即'' ",如果输出,在Console上是看不到效果的。
4、布尔:boolean ,初始值为false
<2>值的大小与对应:
<3>包装类
为了能将基本类型视为对象进行处理,并能连接相关的方法,java为每个基本类型都提供了包装类,如int型数值的包装类integer,boolean型数值的包装类boolean等,这样便可以把这些基本类型转换为对象来处理了。
十种包装类分别为:
Integer 、Long、Short、Byte、Character、Double、Float、Boolean、BigInteger、BigDecmail
其中BigInteger、BigDecimal没有相对应的基本类型,主要应用于高精度的运算,BigInteger 支持任意精度的整数,
BigDecimal支持任意精度带小数点的运算。
然后我们结合代码重点介绍包装类的三点特性。
下面是详细的代码与注释,在笔者“java version "1.8.0_40"”环境下测试通过:
1.Java的缓存机制--Integer自动装箱缓存
3.无符号的(Unsigned)操作方式
<1> 分类与初值:
(六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型)
1、整数:包括int,short,byte,long ,初始值为0
2、浮点型:float,double ,初始值为0.0
3、字符:char ,初始值为空格,即'' ",如果输出,在Console上是看不到效果的。
4、布尔:boolean ,初始值为false
<2>值的大小与对应:
基本型别 | 大小 | 最小值 | 最大值 |
boolean ---->Boolean | ----- | ----- | ------ |
char ---->Character | 16-bit | Unicode 0 | Unicode 2^16-1 |
byte ---->Byte | 8-bit | -128 | +127 |
short ---->Short | 16-bit | -2^15 | +2^15-1 |
int ---->Integer | 32-bit | -2^31 | +2^31-1 |
long ---->Long | 64-bit | -2^63 | +2^63-1 |
float ---->Float | 32-bit | IEEE754 | IEEE754 |
double ---->Double | 64-bit | IEEE754 | IEEE754 |
为了能将基本类型视为对象进行处理,并能连接相关的方法,java为每个基本类型都提供了包装类,如int型数值的包装类integer,boolean型数值的包装类boolean等,这样便可以把这些基本类型转换为对象来处理了。
十种包装类分别为:
Integer 、Long、Short、Byte、Character、Double、Float、Boolean、BigInteger、BigDecmail
其中BigInteger、BigDecimal没有相对应的基本类型,主要应用于高精度的运算,BigInteger 支持任意精度的整数,
BigDecimal支持任意精度带小数点的运算。
然后我们结合代码重点介绍包装类的三点特性。
下面是详细的代码与注释,在笔者“java version "1.8.0_40"”环境下测试通过:
1.Java的缓存机制--Integer自动装箱缓存
/** * 包装类 * Java的缓存机制--Integer自动装箱缓存 */ public class AutoBoxDemo { public static void main(String[] args) { /*Java的缓存机制--Integer自动装箱缓存 * 根据Java的源代码可知,系统会把-128~127之间的整数自动装箱成Integer实例,并放入cache数组中。因此,缓存机制可以大大提高系统的执行速度。 * Java的源代码注释如下: * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * */ Integer inta=11; Integer intb=11;//intb取自cache数组的缓存,与inta是同一个对象 System.out.println("=========="+(inta==intb)); inta=1111; intb=1111;//缓存不下,重新创建对象 System.out.println("=========="+(inta==intb)); } } /*Integer类的源代码片段: * --------------------------------------------------------------------------------------------- private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int 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_VALUE h = 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; } private IntegerCache() {} } -------------------------------------------------------------------------------------------------- * */2.compare方法
/** * 包装类 * compare方法可以使包装类的比较更加方便。 * java1.7开始提供,下面仅仅举Integer类和Boolean类两个例子: */ public class CompareDemo { public static void main(String[] args) { System.out.println("Integer类的compare方法与compareTo方法:"); Integer x=1; Integer y=100; System.out.println(" "+Integer.valueOf(x).compareTo(Integer.valueOf(y))); System.out.println(" "+x.compareTo(y)); System.out.println(" "+Integer.compare(x, y)); System.out.println("---------------------------------------------------------------------"); System.out.println("Boolean类的compare方法:"); Boolean b1=true; Boolean b2=true; System.out.println(" "+Boolean.compare(b1, b2)); } } /*API * ------------------------------------------------------------------------------------------------------- Integer类的compare方法: public static int compare(int x,int y) Compares two int values numerically. The value returned is identical to what would be returned by: Integer.valueOf(x).compareTo(Integer.valueOf(y)) Parameters: x - the first int to compare y - the second int to compare Returns: the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y Since: 1.7 * ------------------------------------------------------------------------------------------------------- Boolean类的compare方法: public static int compare(boolean x,boolean y) Compares two boolean values. The value returned is identical to what would be returned by: Boolean.valueOf(x).compareTo(Boolean.valueOf(y)) Parameters: x - the first boolean to compare y - the second boolean to compare Returns: the value 0 if x == y; a value less than 0 if !x && y; and a value greater than 0 if x && !y Since: 1.7 * ------------------------------------------------------------------------------------------------------- * */
3.无符号的(Unsigned)操作方式
/* Java8为包装类增加了很多无符号的(Unsigned)操作方式,即不考虑符号位. 下面试举几例,具体参考API1.8版本: */ public class UnsignedTest { public static void main(String[] args) { //Integer类 Integer a=-3; Integer b=3; System.out.println("a与b无符号比较相等,返回1 :"+Integer.compareUnsigned(a,b)); System.out.println("a与b比较不相等,返回-1 :"+Integer.compare(a,b)); /* Byte类 byte类型-3 原码为:10000011(最高位为符号位) 反码为:11111100(符号位不变其他位取反) 补码为:11111101(末尾加1) 进行无符号处理得到253 */ Byte c=-3; System.out.println("把c转换为无符号Long型,返回253 :"+Byte.toUnsignedInt(c)); } }
相关文章推荐
- 学习实战全笔记--JavaSE--Annotation(注释)--5种基本注释的用法示例(JDK8)
- 转载自马哥视频学习笔记---Linux Web服务器网站故障分析常用的命令之awk用法的简单示例
- Web Service学习笔记:创建一个示例和WebMethod特性解析
- NodeJS学习系列课程笔记(NodeJs 快速上手+用法示例)
- 【JavaSE学习笔记】泛型,jdk5之后新特性
- 【JavaSE学习笔记】JDK1.8常用新特性
- JavaSE基础学习笔记-提高篇-JDK1.5新特性
- JavaSE基础学习笔记-提高篇-JDK1.5新特性-泛型
- 学习实战完全笔记--JavaSE----流与文件(Streams and Files)
- laravel学习笔记之模型事件的几种用法示例
- 先锋机器人学习笔记_1-2 MobileSim MobileEyes用法及demo示例
- JavaSE--学习实战完全笔记--增强版的线程创建方式--实现Callable接口
- JavaSE学习实战完全笔记--集合的实现细节--Set和Map
- 学习笔记_JavaSE_27_反射、类加载器、JDK5,7,8的新特性
- Sed学习笔记,,,,sed用法
- ExtJs学习笔记(18)_ExtJs嵌入FCK示例
- JAVA核心技术学习笔记(第七版,Ⅱ高级特性)
- SQLServer2008BI新特性学习笔记
- PHP学习笔记1 - 概览、部分特性
- Pro WF Windows Workflow in NET3 -- 学习笔记 -- 状态机 -- 汽车驾驶操作示例