Home  >  Article  >  Java  >  In-depth understanding of Java native types and wrapper types

In-depth understanding of Java native types and wrapper types

黄舟
黄舟Original
2017-03-14 11:44:091607browse


Abstract

This article conducts an in-depth analysis of Java native types and wrapper types, mainly involving the following four Aspects: Basics of native types and wrapper types, concepts and types of literal values, automatic transformation and forced transformation of basic types, and automatic boxing and unboxing mechanisms.


Key points:

  • Primitive types and wrapper types

  • Literal value concept And types

  • Basic types, automatic transformation and forced transformation

  • Autoboxing and Unboxing mechanism(Autoboxing and Unboxing)


1. Primitive types and wrapper types

There are eight basic data types in Java:

     In-depth understanding of Java native types and wrapper types

Special attention needs to be paid to the following points:

  • Integers without character suffix identificationThe default is int type , The floating point number without character suffix identifier defaults to double type ;

  • Only the four basic types

    byte, char, short, int and their wrapper classes (need to be supported by Java5.0/1.5 or above) can be used switch Statement (Only enum type and int type can be applied to switch) , Compilation of other types will report an error;

  • char (two bytes) can use single quotes to represent a single character (can be a Chinese character), such as:

    'Liang',' A';

  • Integer can be composed of binary (

    starts with 0B/0b), octal ( starts with 0 Integers), decimal, hexadecimal (integers starting with 0x or 0X);


2. Literal value

In Java source code, literal value is used to represent a fixed value. Numeric literal values ​​are the most common.

String literal values ​​are also very common and can be regarded as one type. Of course, the special null can also be regarded as a literal value. Literal values ​​can be roughly divided into integer literal values ​​(int type literal value, long type literal value and character literal value) , integer literal value, Floating point typeLiteral value (double type literal value and float type literal value)String literal value, Special literal values Four types.


(1)

Integer literal value

Values ​​that are formally integers are classified as integer literal values. For example: 10, 100000L,

‘B’‘B’, 0XFF These can be called integer literal values. When using, you need to pay attention to the following points:

  • Integer literals can be used in

    decimal, hexadecimal( Starts with 0X/0x), Octal(Starts with 0) and Binary(begins with 0B/0b) to represent. Of course, the base cannot exceed the range of the base system . For example, 09 is illegal, and the base of the octal system can only reach 7.

  • Generally,

    literal values ​​are created of int type, but int literal values ​​can be assigned to byte, short, char, long and int. Of course, The premise is that the literal value is within the target range (Java will automatically complete the conversion) ; If you try to convert a literal value out of range Assigning to a certain type (for example, assigning 128 to the byte type) cannot be compiled and requires forced type conversion.

byte b0 = 128;      
 // Type mismatch: cannot convert from int to bytebyte b1 = 12;       
 // OKbyte b2 = (byte)300;
 // OK 需要强制转换,但表示的数字是截取后的数字(300二进制为100101100,截取后为00101100,即44)char c0 = 5;       
 // OKchar c1 = char(-3);       
 // 编译通过,但打印值为 ?(char 为无符号的)System.out.println(c1);   
 // ?

(2) Floating point literal value

 

Floating point literal value can be simply understood as a decimal number, which is divided into float literal value and double literal value . If F or f is added after the decimal, it means that this is a float literal value, such as 11.8F. If F/f is not added after the decimal (such as 10.4), or D/d is added after the decimal, it means This is a double literal value.

double d1 = 10;     
// OK,自动类型转换double d2 = 11.4;    
// OKdouble d3 = 1.23E3;    
// OK  double d4 = 10D;    
// OK  double d5 = 0.4D;     
// OK float f1 = 10;      
// OK,自动类型转换float f2 = 11.1F;      
// OKfloat f3 = 11.1;      
// Type mismatch: cannot convert from double to float


(3) Character and string literal values

 

Character literal values ​​in Java are enclosed in single quotes, such as ' @','1'. All UTF-16 character sets are included in character literals. For characters that cannot be entered directly, escape characters can be used, such as '\n' for newline characters, or octal or hexadecimal characters. Base represents a character, octal is represented by a backslash plus 3 digits, for example '\141' represents the letter a, and hexadecimal is represented by \u plus 4 as a hexadecimal number, such as '\u0061'. Character a. In other words, by using escape characters, all characters on the keyboard can be represented. Common escape character sequences are: \ddd (octal), \uxxxx (hexadecimal). Unicode characters), \' (single quote), \" (double quote), \ (backslash) \r (carriage return) \n (line feed) \f (form feed) \t (tab) ) \b (backspace character)

 

String literals use double quotes, and string literals can also contain the escape character sequence in character literals.


(4) Special literal value

 

null is a special type (type) that can be assigned to any referenceTypeVariable means that this variable does not reference anything. If a reference type variable is null, it means that the variable is not available.

 

There is also a special class literal, which is represented by type name plus .class, such as String.class. First of all, String is an instance (object) of class Class (java.lang.Class), and "This is a string" is an instance of class String object. Then, class literal is used to represent an object of class Class, for example, String.class is used to represent an object String of class Class. Simply put, class literals are literal values ​​such as String.class and Integer.class. What they represent is the class String and the class Integer. If you print Integer.class, you get class java.lang.Integer. The output of List.class is interface java.util.List. In summary, a class literal is used to represent the type itself.


Special attention should be paid to:

  • in

    in numeric literal You can use underscores in (Starting from JDK7, you can insert one or more underscores in numeric literals (including integer literals and floating-point literals). But Underscores can only be used to separate numbers Underscores can only be used to separate numbers, not characters and characters, nor characters and numbers);

  • int x = 123_456_789;  
    // 在编译的时候,下划线会自动去掉。float f = 1.22_3344;  
     //可以连续使用下划线int = _123;  
     // Errorlong = 1_L;  
     // Error

3. Automatic transformation and forced transformation

1. Automatic transformation

自动转型总原则:byte,short,char(同级)-> int -> long -> float -> double (由低精度到高精度)


(1) 由低精度到高精度的自动转换

具体可分为以下两种情形:

  • 从位数低的类型向位数高的类型转换

    byte b = 1;    char c = 1;    short s = 1;    int i = 1;

    c = b;  // Error,同级
    c = s;  // Error,同级
    s = c;  // Error,同级
    i = c;  // OK
  • 从整型向浮点型的转换———-

  • 从整型向浮点型的转换

    int i = 1;    long t = 1;    float f = 1;    double d = 1;

    f = i;  //  Ok
    f = t;  //  Ok
    d = f;  // Ok

(2) 运算符对基本类型的影响

具体可分为以下两种情形:

1) 当使用 +、-、*、/、%、==、>、运算符对基本类型进行运算时,遵循如下规则:

  两个操作数中,先考虑是否有一个是double类型的。如果有,另一个操作数和结果 将会被转换成double类型。再依次考虑float,long。除此之外,两个操作数(包括byte、short、int、char)都将会被转换成int类型。

byte b1 = 10 ;  
//OK,会检查发现10没有超过byte类型的最大值byte b2 = 12;   
//OK,会检查发现12没有超过byte类型的最大值byte b = b1 + b2; 
//Error,byte类型在计算时会自动提升为int类型,此时就会报错,因为b1+b2实际上是int类型,但是左侧的变量为byte类型。short s1=1; 
//OK,会检查发现1没有超过short类型的最大值s1=s1+1;    
//Error,因为s1+1 结果int,但左侧变量为 short,报错s1++;      
//OK,不会报错,与s1=s1+1不同!!!,会检查发现2没有超过short类型的最大值s1=1+1;   
//OK,1+1 是个编译时可以确定的常量,'+'运算在编译时就被执行了,而不是在程序执行的时候,这个语句的效果等同于s1=2

2) 当使用 +=、-=、*=、/=、%= i++、 ++i 运算符对基本类型进行运算时,遵循如下规则:

  运算符右边的数值将首先被强制转换成与运算符左边数值相同的类型,然后再执行运算,且运算结果与运算符左边数值类型相同。自增(减)运算也类似。

short s1=1; // OK,会检查发现1没有超过short类型的最大值short s2;

s1+=1;    // OK,正确,1首先被强制转换为short型,然后再参与运算,并且结果也是short类型s2 = ++s1;     
// OK,正确,s2的值为2

2、强制转型
  强制转换的格式是在需要转型的数据前加上 “( )”, 然后在括号内加入需要转化的数据类型。主要发生于以下两种情形:

  • 由高精度向低精度转换

  • 一种类型到另一种类型转换,则必须使用强制类型转化(同级之间:byte,short,char)

 byte b = 3; int i = 3; long t = 3; float f = 3; char c = 3; short s = 3;

 i = (int) f;  // OK,由高精度向低精度转换
 t = (long) f;  // OK,由高精度向低精度转换
 b = (byte) i;  // OK,由高精度向低精度转换

 i = b; // OK,由低精度向高精度转换,自动转型
 System.out.println(c==s);  // OK,true,c 和 s 自动转型为int,然后比较

 b = (byte) s;  // OK,一种类型到另一种类型转换
 c = (char) b;  // OK,一种类型到另一种类型转换
 c = (char) s;   // OK,一种类型到另一种类型转换

  特别需要注意的是,强制转换常常会导致二进制位的截取,甚至会导致意想不到的结果:

 int i = 128; byte b = (byte)i;
 System.out.println(b);           // -128(即-0)

四. 自动装箱与拆箱(Autoboxing and Unboxing)

1、什么是装箱?什么是拆箱?

  Java为每种基本数据类型都提供了对应的包装器类型。在 Java SE5 之前,如果要 创建一个数值为10的Integer对象,必须这样进行:

Integer i = new Integer(10);

  而从 Java SE5 之后就提供了自动装箱的特性,如果要 创建一个数值为10的Integer对象,只需要这样就可以了:

Integer i = 10;

  这个过程中会自动根据数值创建对应的 Integer对象,这就是装箱

  那什么是拆箱呢?顾名思义,跟装箱对应,就是自动将包装器类型转换为基本数据类型:

Integer i = 10;  //装箱int n = i;   //拆箱

  简单一点说,装箱就是自动将基本数据类型转换为包装器类型拆箱就是自动将包装器类型转换为基本数据类型


2、装箱和拆箱是如何实现的

 上一小节了解装箱的基本概念之后,这一小节来了解一下装箱和拆箱是如何实现的。我们就以Interger类为例,下面看一段代码:

public class Main {
    public static void main(String[] args) {

        Integer i = 10;        int n = i;
    }
}

反编译class文件之后得到如下内容: 

        In-depth understanding of Java native types and wrapper types
           
  从反编译得到的字节码内容可以看出,在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法

  对于其他的包装器类,比如Double、Character,也同样适用。

  因此,可以用一句话总结装箱和拆箱的实现过程:

  装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的(xxx代表对应的基本数据类型)。


3、valueOf、xxxValue 方法在JDK中的实现

(1) 在 Byte,Character,Short,Integer,Long 中的实现(以 Integer 为例)

  • public static Integer valueOf(int i)类方法

public static Integer valueOf(int i) {        
if(i >= -128 && i <= IntegerCache.high)            
return IntegerCache.cache[i + 128];        
else
            return new Integer(i);
            
    }    private static class IntegerCache {
        static final int high;        
        static final Integer cache[];        
        static {                                         
        // 静态代码块  
            final int low = -128;            
            // high value may be configured by property
            int h = 127;            
            if (integerCacheHighPropValue != null) {                
            // Use Long.decode here to avoid invoking methods that
                // require Integer&#39;s autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);                
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            high = h;

            cache = new Integer[(high - low) + 1];            
            int j = low;            
            for(int k = 0; k < cache.length; k++)                      
            // 初始化  
                cache[k] = new Integer(j++);
        }        private IntegerCache() {}
    }

  在装箱时,valueOf方法会被自动调用:如果整型字面值在[-128,127]之间,便返回 IntegerCache.cache(在类加载时就自动创建) 中已经存在的对象的引用;否则,创建一个新的Integer对象并返回。

  • public int intValue()实例方法

public int intValue() {    return value;
}

 从这段代码可以看出,在拆箱时,Integer对象会自动调用其 intValue 方法,返回该对象对应的 int 值。
 
下面代码可以很好说明这一点:

public class Main {
    public static void main(String[] args) {

        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;

        System.out.println(i1==i2);          // true
        System.out.println(i3==i4);          // false
    }
}

(2) 在 Float, Double 中的实现(以 Double 为例)

  • public static Double valueOf(double d) 类方法

   public static Double valueOf(double d) {        return new Double(d);
    }

  在装箱时,valueOf方法会被自动调用,从而创建相应的Double对象并返回。

  • public int intValue()实例方法

 public double doubleValue() {    return value;
}

  从这段代码可以看出,在拆箱时,Double对象会自动调用其 doubleValue 方法,返回该对象对应的 double 值。
  
  下面代码可以很好说明这一点:

public class Main {
    public static void main(String[] args) {

        Double i1 = 100.0;
        Double i2 = 100.0;
        Double i3 = 200.0;
        Double i4 = 200.0;

        System.out.println(i1==i2);          // false
        System.out.println(i3==i4);          // false
    }
}

  为什么Double类的valueOf方法会采用与Integer类的valueOf方法不同的实现呢?原因很简单,在某个范围内的整型数值的个数是有限的,而浮点数却不是


(3) 在 Boolean 中的实现

  • public static Boolean valueOf(boolean b) 类方法

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {        
return (b ? TRUE : FALSE);
}

  在装箱时,valueOf方法会被自动调用,从而创建相应的Boolean对象并返回。

  • public boolean booleanValue()实例方法

 public boolean booleanValue() {    return value;
    }
}

  从这段代码可以看出,在拆箱时,Boolean对象会自动调用其 booleanValue 方法,返回该对象对应的 boolean 值。
  
  下面代码可以很好说明这一点:

public class Main {
    public static void main(String[] args) {

        Boolean i1 = false;
        Boolean i2 = false;
        Boolean i3 = true;
        Boolean i4 = true;

        System.out.println(i1==i2);          // true
        System.out.println(i3==i4);          // true
    }
}

 总之,

  • Integer、Short、Byte、Character、Long 这几个类的valueOf方法的实现是类似的,有限可列举,共享[-128,127]

  • Double、Float的valueOf方法的实现是类似的,无限不可列举,不共享

  • Boolean的valueOf方法的实现不同于以上的整型和浮点型,只有两个值,有限可列举,共享


4、Integer i = new Integer(xxx)Integer i =xxx;的区别

  • 第一种方式不会触发自动装箱的过程,而第二种方式会触发;

  • 在执行效率和资源占用上的区别。第二种方式的执行效率和资源占用在一般性情况下([-128,127])要优于第一种情况(注意这并不是绝对的)。


5、“==”运算符

  当使用“==”运算符在基本类型和其包装类对象之间比较时,涉及到自动装箱、拆箱机制,遵循如下规则:

   1). 只要两个操作数中有一个是基本类型或表达式(即包含算术运算符),就是比较它们的数值是否相等。
   2). 否则,就是判断这两个对象的内存地址是否相等,即是否是同一个对象。


// 代码片段1public class Main {    public static void main(String[] args) {

        Integer i01=59;        int i02=59;
        Integer i03=Integer.valueOf(59);
        Integer i04=new Integer(59);

        System.out.println(i01==i02);  // true,拆箱
        System.out.println(i01==i03);  // true,同一对象
        System.out.println(i03==i04);  // false,不同对象
        System.out.println(i02==i04);  // true,拆箱
    }
}

// 代码片段2public class Main {    public static void main(String[] args) {

        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Integer d = 3;
        Integer e = 321;
        Integer f = 321;
        Long g = 3L;
        Long h = 2L;

        System.out.println(c==d);  // true
        System.out.println(e==f);  // false
        System.out.println(c==(a+b));  // true
        System.out.println(c.equals(a+b));  // true
        System.out.println(g==(a+b));  // true
        System.out.println(g.equals(a+b));  // false
        System.out.println(g.equals(a+h));  // true
    }
}
  • 第一个和第二个输出结果没有什么疑问;

  • 第三个打印语句由于 a+b 包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),因此它们比较的是数值是否相等;

  • 对于c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较;

  • 对于g==(a+b),会分别触发 Integer 和 Long 的自动拆箱过程,然后 int 自动转为 long,进行比较;

  • 对于g.equals(a+b),最终会归结于 Long对象与Integer对象的比较,由于二者不为同一类型,直接返回 false ;

  • 对于g.equals(a+h),最终会归结于 Long对象与Long对象的比较,由于 -128


6、小结

  • 使用“==”运算符在基本类型和其包装类对象之间比较时,只要两个操作数中有一个是 基本类型 表达式(即包含算术运算符),就是比较它们的数值是否相等。否则,就是判断这两个对象的内存地址是否相等,即是否是同一个对象

  • 如果一个 方法中参数类型为原生数据类型 ,所传入的参数类型为其封装类对象,则会自动对其进行 拆箱 ;相应地,如果一个方法中 参数类型为封装类型对象 ,所传入的参数类型为其原始数据类型,则会自动对其进行 装箱 ,例如上述的equals方法。

      至于什么时候装箱,什么时候拆箱主要取决于:在当前场景下,你需要的是引用类型还是原生类型。若需要引用类型,但传进来的值是原生类型,则自动装箱(例如,使用equals方法时传进来原生类型的值);若需要的是原生类型,但传进来的值是引用类型,则自动拆箱(例如,使用运算符进行运算时,操作数是包装类型)。

The above is the detailed content of In-depth understanding of Java native types and wrapper types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn