学习实战全笔记--JavaSE--包装类的特性--用法示例(JDK8)

来源:互联网 发布:大数据时代读后感5000 编辑:程序博客网 时间:2024/05/17 12:23

      我们知道Java是面向对象的语言,但是为了照顾程序员对以前编程语言的习惯,增加了八种基本数据类型(他们不是对象)如下:

    <1> 分类与初值:

     (六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型)
       1、整数:包括int,short,byte,long ,初始值为0
       2、浮点型:float,double ,初始值为0.0
       3、字符:char ,初始值为空格,即''  ",如果输出,在Console上是看不到效果的。
       4、布尔:boolean ,初始值为false

      <2>值的大小与对应:

 基本型别大小最小值最大值boolean    ---->Boolean
----------------char           ---->Character
16-bitUnicode 0Unicode 2^16-1byte           ---->Byte
8-bit-128+127short          ---->Short
16-bit-2^15+2^15-1int              ---->Integer
32-bit-2^31+2^31-1long          ---->Long
64-bit-2^63+2^63-1float         ---->Float
32-bitIEEE754IEEE754double    ---->Double
64-bitIEEE754IEEE754
        <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自动装箱缓存

/** * 包装类 * 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 compareReturns:    the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > ySince:    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 compareReturns:    the value 0 if x == y; a value less than 0 if !x && y; and a value greater than 0 if x && !ySince:    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));}}


0 0