The eight basic types in Java

The data type boolean byte char short int long float double
Packing type Boolean Byte Character Short Integer Long Float Double
bit 32 8 16 16 32 64 64 64

Two guidelines for basic types:

  • The default type for integer data is int, and the default type for floating point data is double
  • Basic data types can be cast automatically from small (bytes) to large, requiring a type cast from large to small

Boolean types are converted to int by the JVM at compile time, where true is a constant value of 1 and false is 0, as in Boolean a = true; If you look at the bytecode (javap-verbose xxx.class), you will find that the iconst_1 instruction pushes the int constant value 1 onto the stack, so Boolean requires 4 bytes to store. The reason for using int is that for today’s 32-bit processors (cpus), data is processed 32 bits at a time. Boolean arrays are compiled as byte arrays, so each Boolean element in the array takes only one byte.

Basic types have corresponding packing types. The conversion between basic types and packing types is completed automatically:

Integer a = 5; Integer.valueof (2) int b = a; // Unbox integer.intValue ()Copy the code

In Java 8, most primitive types have cached values, such as Integer, which caches values from -128 to 127 through the cache[] of its internal IntegerCache class.

/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties inthe * sun.misc.VM class. */ private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; . }Copy the code

When an Integer is initialized using boxing, objects in the cache range are referenced if the initial value is in the cache range. As you can see from the source code note above, Integer can set its cache size by adding -xx :AutoBoxCacheMax=

when starting the JVM, but there is no corresponding setting for the other basic types.

Integer a = 5;
Integer b = 5;
System.out.println(a==b); //true
b = new Integer(5); // 为5分配了新的空间,所以与缓存5的空间地址不同
System.out.println(a==b); //false
a = 255;
b = 255;
System.out.println(a==b); //false
Copy the code

The cache pool values for the wrapper types for each base type (Double and Float are not cached) are as follows:

  • Boolean: Cache fields true, false
  • Byte: internal class ByteCache caches all bytes (-127-128)
  • Short: internal class ShortCache cache -127-128
  • Integer: internal class IntegerCache cache -127-128
  • Long: inner class LongCache cache -127-128
  • Character: Internal class CharacterCache The ASCII Character value corresponding to 0 to 127

Read different bit operations from the number of bytes

char a = 'a'; // 'a'Int b = 13; long e = a + d;Copy the code

So the question is, what is e? How do the types get converted during the operation? Please be sure to let me explain one by one according to the following picture:

  1. Bipush converts a to an int value (the ASCII value corresponding to ‘a’) and istore_1 retrieves the top int value (that is, the a value) and stores it in local variable 1
  2. Bipush pushes b(which is itself an int without conversion) onto the stack, and istore_2 takes the top int value (that is, the value of b) and stores it in local variable 2
  3. Iload_1 and ILoad_2 stack the int values of local variables 1 and 2,iadd add the two int values at the top of the stack and push the result, i2L convert int to long(not I2 11), and lstore_3 store the stack long value to local variable 3

If you ask me what good does it do to know this? Your first instinct will be to doge, but you’ll also learn more about basic data type operations.