Basic part: java basic types


1: Several basic types of Java

keyword type Bits (8 bits, 1 byte) Value range (representing range)
byte integer 8 -2^7 ~ 2^7-1
short integer 16 -2^15 ~ 2^15-1
int integer 32 -2^31 ~ 2^31-1
long integer 64 -2^63 ~ 2^63-1
float Floating point number 32 3.402823e+38 ~ 1.401298e-45
double Floating point number 64 1.797693e+308~ 4.9000000e-324
char Text type 16 0 ~ 2^16-1
boolean Boolean value 32/8 true/false

What is the size of a Boolean

  • 1bitAfter compiling, Boolean uses 1 and 0 to store. Theoretically, it only needs 1 bit to store
  • 1byte: the minimum unit of data processed by a computer is 1 byte, which is stored in the lowest bit of a byte, and the rest is filled with 0. If the value is true, the stored binary is 0000 0001, and if false, 0000 0000
  • 4byte or 1btye: Java virtual machine has no special bytecode instruction for boolean type, the Boolean operated by expression is replaced by int data type after compilation, and the Boolean array is compiled into byte array

Positive solutionThe correct answer in Java should be that the boolean type is 4 bytes when used alone, and 1 byte in an array. But why does the virtual machine not use byte or short instead of Boolean but int, which is not more memory saving? For a 32-bit processor, int processes 32-bit data at a time, and CPU addressing is also a 32-bit search, which has the characteristics of efficient storage (if you have a better understanding, let’s talk about it)

2: In a 64 bit JVM, what is the length of the int type

32-bit; int is a 32-bit type that does not change with the number of bits in the system or JVM

3: Can a char type variable store a Chinese character? Why

Java’s default encoding is Unicode, each character takes up two bytes, char is 16 bit type, Chinese storage needs two bytes, so it can store Chinese characters.

4: Floating point float and double precision floating point double representation

The binary representation of floating point numbers consists of three parts

  • Sign bit
  • Index level
  • The mantissa is

Float, double binary structure

type Sign bit Index (E) Coda (m)
float 1 8 23
double 1 11 52
  • The symbol bit part is used to store digital symbols, distinguish positive and negative numbers, 0 positive and 1 negative
  • Index storage index, index also has positive and negative, index to determine the size range

    • The index is signed, but the calculation of signed integer is more troublesome than that of unsigned integer. Therefore, the actual storage is to convert the index into an unsigned integer. The range of 8bit is 0-255. The offset 127 needs to be subtracted from the real index. Range (- 126 ~ 128)
  • Mantissa stores the decimal part to determine the precision of floating-point numbers. The larger the number that decimal can represent, the higher the precision and the more accurate the value

    • The last digit of float is 23, 2 ^ 23 = 83886088388608, which is a 7-digit decimal system. If you add the ignored integer digit, you can represent up to 8-digit decimal system. But it can absolutely guarantee that the effective digit is about 7 decimal digits; the tail digit of double is 52, 2 ^ 52 = 4503599627370496, and the 16 digit number plus the integer digit 2 ^ 53 is also a 16 digit number, so it can absolutely guarantee that the effective digit is exactly 15 decimal digits
  • Storage example of 15.625:

    • 15.625 to binary 1111.101
    • Move 1111.101 to the right by three places, leaving 1 decimal place: 1.111101 * 2 ^ 3
    • Bottom digit: because it must be 1 before the decimal point, you only need to record the number of digits after the decimal point. At this time, the bottom digit is 1111 01, and the low zeros are 111 1010 0000
    • Index bit: the index is 3, plus 127 (minus 127 in case of inversion) to get 130, and the binary of index bit is 1000 100
    • Sign bit: positive 0
    • The binary form of 15.625 in memory is expressed as

      Symbol index Mantissa
      0 1000 0010 111 1010 0000 0000 0000 0000

5: Basic type corresponding to the packaging class, difference

Basic types Packaging
boolean Boolean
short Short
byte Byte
int Integer
long Long
float Float
double Double
char Character
  • For Java, where everything is an object, why is there a basic type? Because when Java generates objects, it usually needs to create and maintain them in the heap, and then use them through stack reference. But for simple small variables, it’s too troublesome to create and reuse them in the heap
  • Why is there packaging

    • The wrapper class wraps the basic type to make it have the property of object. It can add properties and methods to enrich the operation of basic type
    • For generic programming, or using the collection collection, you need to wrap the class. Because of ArrayList, the generic type of HashMap cannot specify the base type
    • The difference is that the basic type can be declared directly, and the wrapper class needs to be created in the heap and then used by reference; the default initial value of the basic type is int = 0, while Boolean is true / false and cannot be assigned to null; the default initial value of the wrapper class is null
  • Note: byte, int, short, long direct assignment (or use valueof) such as integer x = value (value in – 128 ~ 127) is a direct reference to the object in the constant pool, at this time object comparison = = and equals are true; character declaration value in 0 ~ 127 is a reference to the constant pool object

Basic part: java basic types

6: Automatic conversion of basic types

The automatic conversion rules are as follows

  • There is no implicit conversion of boolean type to other types (non automatic encapsulation type)
  • Automatic promotion of integer type

    • Byte – > (short / char) – > int – > long (automatic lifting chain)
    • Data type representing low range can be implicitly promoted to data type representing high range (byte B = 1; short s = B;); no compilation error
    • Both short and char are 16 bits, but they cannot be implicitly converted
  • Automatic conversion from character data to integer data

    • Char is an unsigned type, representing a range of (0 ~ 2 ^ 16-1), which can be implicitly converted to int or long
  • Integer and character data can be automatically converted to floating point data

    • Because the number of significant digits that floating-point type can hold is limited, we need to consider the problem of significant bits after conversion

Basic part: java basic types

  • Automatic lifting of floating point data

    • There is the problem of precision error in the conversion of float to double, and there is the problem of precision loss in the conversion of double to float

Basic part: java basic types

7: Short S1 = 1; S1 = S1 + 1; what’s wrong? Short S1 = 1; S1 + = 1; what’s wrong?

  • The default type of 1 in S1 = S1 + 1 is int. the low range type S1 in the expression will be converted to int by default to get the result of int type. Finally, the result of int type cannot be converted to short implicitly, and compilation will report an error
  • S1 + = 1; there is an implicit forced conversion S1 + = 1 – > S1 = (short) S1 + 1; compilation will not report an error

8: What might be the problem with different base type casts

  • Floating point to integer, precision loss, data overflow
  • The integer with a large range of values is converted to the integer with a small range of values, resulting in data overflow and high bit loss

9: Float f = 3.4; is it correct?

In Java, floating-point numbers without suffix modification are of double type by default. Double type cannot be converted to float implicitly. Compilation will report an error

10: What does the expression 3 * 0.1 = = 0.3 return? True or false?

Floating point type has precision problem. The tail digit of double data obtained by 3 * 0.1 is different from that of 0.3, false

11: Floating point numbers and BigDecimal

  • Floating point type uses binary storage, regardless of float (7), double (15), its significant bits are limited, there are rounding errors, precision is easy to be missing
  • There is a serious problem of accuracy when decimal is converted to floating point number. Then, can we expand the decimal decimal to N times the integer dimension for calculation, and retain its precision digits? This is BigDecimal
  • BigDecimal is based on BigInteger to process calculation. There is an int [] MAG inside BigInteger, which represents the principle of BigInteger, the original byte array that stores positive numbers
  • Avoid floating-point type construction when constructing BigDecimal, which will cause precision problems. Try to create BigDecimal with strings, or use the valueof method

    BigDecimal data= new BigDecimal(0.1);
    System.out.println("data:" + data);
  • When BigDecimal performs division without integer division, it will throw an arithmeticexception exception and need to specify the precision
  • Specify the number of precision bits and the rounding mode

12: Can switch statement act on byte type variable, long type variable and string type variable?

  • Switch (expr) when expr is needed, the int type, and byte, char, short will be automatically promoted to int, so it can be used on the switch keyword. Long, float, double cannot be automatically converted to int, compilation will report an error
  • String can’t be automatically converted to int, and it can’t be used in switch before 1.7. However, after 1.7, JDK supports string, through String.hashCode Returns an int type and uses it again in case String.equals compare

    //Syntax Decompilation
        case 3556498:

13: Can a double value be assigned to a long type variable without coercion

No, because double is larger than long. No cast will compile and report an error. If you use the + = operator, compilation will not report an error, but there is an implicit cast

Pay attention to the official account, and we will exchange with you.

Basic part: java basic types

Reference article