2. The basic data types of java

basic data types in java

1. Conversion between basic types

    The char type cannot be automatically converted to any other basic type
    byte  char short int long float double boolean
    byte<(char = short)<int<long<float<double
    char: unsigned integer.

2. Situations that must be forced

    Types with small capacity are automatically converted to types with capacity .

    byte b= 10;
    char ch = (char)b;//byte----->int------>char    

char is an unsigned integer, so the range is larger than the short range.

    short sh = 10;
    char ch2 = (char)sh

Byte, short, and char are first converted to int during calculation, and the mandatory conversion character is required when converting a large-capacity (int) to a small-capacity type (byte).

    byte b2 = 10;
    byte b3 =10;
    byte b4 =(byte)(b2+b3);	

3. Unboxing and packing

Boxing is to convert the basic types of java into corresponding wrapper classes.

Unboxing is the conversion of a wrapper class into the corresponding primitive type.

autoboxing

Autoboxing is to call the valuetOf() method to convert the primitive type into an object.

Integer a2 = Integer.valueOf(5);

Automatic unboxing

Automatic unboxing is to call intValue() to convert Integer to the corresponding basic type.

int a3 = a2.intValue();

explicit boxing

    Integer i2 = (Integer)10;

Explicit unboxing

    int a4 = (int) a3

Guess you like

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