【130天】尚学堂高淇Java300集视频精华笔记(63-64)

第63集:经常使用类包装类IntegerNumberJDK源码分析

基本数据类型的包装类

  1. JAVA并非纯面向对象的语言。Java语言是一个面向对象的语言,可是Java中的基本数据类型倒是不面向对象的。可是咱们在实际使用中常常须要将基本数据转化成对象,便于操做。好比:集合的操做中。例如使用Map对象要操做put()方法时,须要传入的参数是对象而不是基本数据类型。java

  2. 为了解决这个不足,在设计类时为每一个基本数据类型设计了一个对应的类进行表明,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。缓存

  3. 包装类均位于java.lang包,包装类和基本数据类型的对应关系以下表所示:
    app

  4. 在这八个类名中,除了Integer和Character类之后,其它六个类的类名和基本数据类型一直,只是类名的第一个字母大写便可。源码分析

包装类的用途

  1. 做为和基本数据类型对应的类的类型存在,方便涉及到对象的操做。测试

  2. 包含每种基本数据类型的相关属性如最大值、最小值等,以及相关的操做方法(这些操做方法的做用是在基本类型数据、包装类对象、字符串之间提供转化!)。spa

包装类的共同方法

全部的包装类(Wrapper Class)都有共同的方法,他们是:设计

/**
 * 测试Integer的用法,其余包装类相似
 */
 
package com.test063;

public class Test063 {
    public static void main(String[] args){
    
        //基本类型转换成为Integer类型
        Integer int1 = new Integer(10);
        Integer int2 = Integer.valueOf(10);
        
        //Integer对象转换成int类型
        int int3 = int1.intValue();
        
        //字符串转化成Integer对象
        Integer int4 = new Integer("234");
        
        //Integer对象转换为字符串
        String str1 = int2.toString();
        
        //一些常见的int类型相关的常量
        System.out.println("int能表示的最大整数:"+ Integer.MAX_VALUE);//正2的31次方
        System.out.println("int能表示的最小整数:"+ Integer.MIN_VALUE);//负2的31次方
        
        //数值转换位字符串
        String str = 234 +"";
        System.out.println(str);
        
    }
}

第64集:经常使用类/自动装箱和拆箱/缓存处理

自动装箱和拆箱的含义(autoboxing,unboxing)

就是编译器将基本类型和包装类进行自动的互相转换。JDK5.0后,将自动装箱/拆箱引入java中。
   code

自动装箱和拆箱的过程    


  1. 自动装箱的过程:每当须要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中。对象

  2. 自动拆箱的过程:每当须要一个值时,被装箱对象中的值就被自动地提取出来,不必再去调用intValue()和doubleValue()方法。rem

自动装箱与拆箱的功能事实上是编译器来帮您的忙,编译器在编译时期依您所编写的语法,决定是否进行装箱或拆箱动做。例如:

Integer i = 100;
// 至关于编译器自动为您做如下的语法编译:
Integer i = new Integer(100);

自动装箱和拆箱现象存在的证实

  1. 自动装箱与拆箱的功能是所谓的“编译器蜜糖”(Compiler Sugar),虽然使用这个功能很方便,但在程序运行阶段您得了解Java的语义。例以下面的程序是能够经过编译的:

    Integer i = null;
    int j = i;
    
    //这样的语法在编译时期是合法的,可是在运行时期会有错误,由于这种写法至关于:
    
    Integer i = null;
    int j = i.intValue();
  2. null表示i没有参考至任何的对象实体,它能够合法地指定给对象参考名称。因为实际上i并无参考至任何的对象,因此也就不可能操做intValue()方法,这样上面的写法在运行时会出现NullPointerException错误。

/**
 * 测试自动装箱和拆箱
 * 结论:虽然很方便,可是若是不熟悉特殊状况,可能会出错!
 */
 
static void testBoxing(){
Integer b = 23;    //自动装箱
int a = new Integer(20);    //自动拆箱
 

//自动装箱和拆箱,其实是编译器替咱们完成了代码的自动编译,好比:Integer b = 23, 其实运行时执行的仍然是:Integer b = new Integer(23);

//自动装箱拆箱时,对于-128-127之间的值,编译器仍然会把它当作基本类型处理。
Integer h = 100; Integer u = 100;
Integer h2 = 200; Integer u2 = 200;
if(h==u){
System.out.println("100等于");
}
if(h2==u2){
System.out.println("200等于");
}

}

自动拆装箱的范围

//注意一个自动拆装箱的范围,在[-128,127]这个区间,编译器仍然将数值做为基本类型,而非一个对象,这是为了提升效率。
        Integer d1 = 1234;//等同于Integer d1 = new Integer(1234);
        Integer d2 = 1234;//等同于Integer d2 = new Integer(1234);
        System.out.println(d1==d2);    //    false
        System.out.println(d1.equals(d2));    // true
        
        Integer d3 = 123;
        Integer d4 = 123;
        System.out.println(d3==d4);// true 作字符运算时,依然看成基本数据类型处理
        System.out.println(d3.equals(d4));// true

自动装箱和自动拆箱的课堂案例代码

package com.test064;

public class Test064 {
    public static void main(String[] args){
        Integer i1 = new Integer(2);
        Integer i2 = 2;//JDK5.0后,编译器帮咱们改进代码,至关于Integer i2 = new Integer(2);
        Integer i3 = 2000;
        int c1 = i3;//自动拆箱,编译器帮咱们改进:i3.intValue();
        System.out.println(c1);
        
        
        
    }
}
相关文章
相关标签/搜索