Java study notes (six)--packaging, basic types, unboxing, packing

Basic types and wrapper classes

        1. There are 8 basic types in java, and there are 8 packaging types corresponding to them. The packaging class is the packaging for the native data type.

                       basic type                           type of packaging
byte Byte (subclass of Number)
short Short (subclass of Number)
int Integer (subclass of Number)
long Long (subclass of Number)
float Float (subclass of Number)
double Double (subclass of Number)
character Character (subclass of Object)
boolean Boolean (subclass of Object)

2. Difference

  • Basic types are directly used to store values ​​and are placed on the stack for quick and easy use. The wrapper type is a class, and its instance is an object, which is placed in the heap.
  • Base types are not objects and therefore have no methods, wrapper types are classes and therefore have methods
  • The basic type can be assigned directly, and the packaging type needs to be created using the new keyword
  • The initial value of the wrapper type is null, and the initial value of the basic type is not null, which depends on the type

3. Why do you need a packaging type

 The wrapper type wraps the value of the basic type in the object, so that the method of the object can be used to operate the basic type. The conversion between types needs to be done using the method of the wrapper type, so the wrapper type is required.

               Packaging class                  box                 Unboxing
Byte

valueOf(String s) is boxed into a wrapper class

valueOf(String,int) is boxed into a wrapper class according to the specified base

byteValue()
Short valueOf() shortValue()
Integer valueOf() intValue()
Long valueOf() longValue ()
Float valueOf() floatValue ()
Double valueOf() doubleValue()
Character valueOf() charValue ()
Boolean valueOf() booleanValue()

Using the parseXXX method of the wrapper class can also implement conversion between different types, such as converting a numeric string to an int type:

        int i = Integer.parseInt("23");

Unboxing and packing

      Boxing: The process of converting a primitive type into a wrapper class

      Unboxing: The process of converting a wrapper class into a primitive type

      Automatic unboxing is provided after jdk1.5

get a chestnut

public class SimpleTest {
    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
    }
}

  ① The result is true, ② is false, what is the reason? (My jdk is version 1.8)

   Integer i1 = 100; the operation performed is 100 first through the valueOf() method for boxing operation, the source code of Integer.valueOf() is as follows:

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
 Integer is special. Between -128 and 127, it will go to the cache. The number that is not in this range will go to new Integer(i), that is, a new object will be created. Therefore, when 100 is compared, it is true, and when 200 is compared, it is true. is false

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325936562&siteId=291194637