Java basic - basic data types

Java8 basic data types

Types of

size

range

Defaults

byte

8

-128 - 127

0

short

16

-32768 – 32767

0

int

32

-2147483648-2147483647

0

long

64

-9233372036854477808-9233372036854477807

0

float

32

-3.40292347E+38-3.40292347E+38

0.0f

double

64

-1.79769313486231570E + 308-1.79769313486231570E + 308

0.0d

char

16

\u0000 - u\ffff

\ U0000

boolean

16

true/false

false

  • byte : byte is 8-bit signed integer in two's complement code representation, 1-byte, the default value is 0, it is mainly used in a large array save space, the main alternative integers, since variable byte space occupied by only four int one points. The range of [-2^{7},2^{7}-1]
  • short : a short 16-bit signed, two's complement integer of 2 bytes, the default value is 0, the same space can be saved as byte, a variable short is one-half the space occupied by the int. Its range  [-2^{15},2^{15}-1].
  • int : int 32-bit signed integer in two's complement code representation, it occupies 4 bytes, the default value is 0, the general default integer variable type int. Its range  [-2^{32},2^{32}-1] .
  • Long : Long signed 64-bit, two's complement integer identifier, 8 bytes, the default value is 0L, usually in the large integer arithmetic system (int cause data using arithmetic overflow errors). Its range [-2^{32},2^{32}-1].
  • a float : a float single precision 32-bit IEEE 754 standard composite ,, float, occupies 4 bytes, the default value 0.0f. Since the single-precision floating-point, so the rounding error generated search terms, it can not be used to represent the exact value. The default type is a floating point double, so cast the following format: float a = 6.36F.
  • Double : Double double-precision 64-bit, IEEE 754 standard floating-point complex, occupies 8 bytes, the default value is 0.0d, default type float. Due to accuracy problems, double the same can not accurately represent the amount of value.
  • char : char is a single 16-bit Unicode characters, using the '' represents a character occupies 2 bytes, the default value is '\ u0000' (0), the minimum value is '\ u0000', the maximum value is '\ uFFFF' (65535). char type can be used as an integer, each character corresponds to a number, corresponding to the particular bin number may test \mathbf{ASCII}table.
  • Boolean : Boolean type is 16-bit data, 2 bytes, only two values true and false, this type are commonly used as a marker, the default value is false.

 

Java is an object-oriented language, but the basic data types in Java is not object-oriented, that is, when using the eight basic data types can not be used as an object, in order to solve this problem, the designer of the eight basic data type separately packaged as a class, these classes are called eight packaging (wrapper class).

Basic data types

Wrapper class

int

Integer

char

Character

float

Float

double

Double

byte

Byte

short

Short

long

Long

boolean

Boolean

Automatic boxing and unboxing automatic

Briefly, autoboxing is a basic data types will be converted into a wrapper class object, in the process JVM calls the valueOf () method.

Integer a1 = new Integer(11);
Integer a2 = 11;        //自动装箱
Integer a3 = Integer.valueOf(11);

The first line of code is essential to create an Integer object, the second is the automatic packing line, the third line is the actual operation of the second row.

Automatically unpacking the packing and automatic contrast, the object is again reduced to a packaging basic types of data. For example, commonly used output statement:

System.out.println(i+1);

i is an integer obtained by the object of our automatic packing, and this object is the operation can not be performed directly, we int then calculates the integer by the object into basic data types, the process is automatically unpacking.

PS: automatic automatic packing and unpacking is done automatically by the JVM, JVM whether to perform boxing and unboxing decision in accordance with the syntax compiler. Automatic boxing and unboxing is jdk1.5 before the introduction of new features, if jdk version is less than 1.5, then, it is not achievable.

Automatic packing may also serve to save memory, for values ​​between -128 and 127, after which there will be boxed Integer object to be reused in memory, there is always an object (Flyweight), but not in this range the value will not be reused every time a new Integer object.

Note that references to the wrapper class is not empty, the runtime throws a null pointer exception. Although this is a complex Java syntax specification, the compiler can be normal through, but did not function properly.

Integer a = null;
int b = a;

Source analysis

Integer, for example, we look at the source code in Java on boxing unboxing

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
        // 没有设置的话,IngegerCache.high 默认是127
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
}

For the above mentioned values of -128 to 127 exist in memory to be reused when the packing can be seen in the source code, when i is IntegerCache.low and IntegerCache.high is between, generates in memory a unique the object, the second minor when you get it, it will give the object the first time you generate the address, but not in this range is to return a new object. IntegerCache is autoboxing pool, pool size autoboxing is how to define it? One such class Integer.java the inside:

// jdk 1.8
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() {}
    }

This IntegerCache class defines the size of the Integer autoboxing pool, you can see from the source code, that is, the lower bound is written IntegerCache.low dead -128, while the upper bound by the parameters integerCacheHighPropValue decoding come, which means that we can change this parameter changes the size of the pool automatic packing. Autoboxing default pool size as follows:

type of data range
Integer,Short,Long -128~127
Byte -128~127
Character 0~127
Float,Double

No boxing pool

That is in the above range, when using == two objects are equal.

 

Guess you like

Origin blog.csdn.net/Wyong9802/article/details/92958859