Time：2020-8-28

# 1: The size of several basic data types in Java

keyword type Bits (8 bits, 1 byte) Value range (indicated 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

• 1bit: Boolean is compiled using 1 and 0 storage, theoretically only 1 bit can be stored
• 1byte: the minimum unit of data processed by the computer is 1 byte, which is stored in the lowest bit of a byte, and the others are filled with 0. If the value is true, the stored binary is 0000 0001 and false is 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 Boolean array is compiled into byte array

Positive solutionIn Java, the correct answer should be that boolean type is used alone, which is 4 bytes, and in array, it is 1 byte. But why don’t virtual machines use byte or short instead of Boolean instead of int, which is not more memory saving? For 32-bit processor, int can process 32-bit data at a time, and CPU addressing is also 32-bit lookup, which has the characteristics of efficient storage (if you have a better understanding, let’s share with each other)

# 2: What is the length of the int type in a 64 bit JVM

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

# 3: Can char type variables store a Chinese character? Why

In the Chinese Unicode encoding mode, each character takes two bytes. Char is a 16 bit type. Chinese storage requires two bytes, so Chinese characters can be stored.

# 4: Float and double representation of floating point numbers

The binary representation of floating-point numbers consists of three parts

• Sign bit
• Exponential bit
• The mantissa is

Binary structure of float and double

type Sign bit Bit index (E) Last digit (m)
float 1 8 23
double 1 11 52
• The sign bit part is used to store the number symbol, distinguish positive and negative numbers, 0 positive and 1 negative
• The index bit stores the index, the index also has positive and negative, and the index determines the size range

• The index is signed, but the signed integer is more difficult to calculate than the signed integer. Therefore, the actual storage is to convert the index to an unsigned integer, with the range of 8bit ranging from 0 to 255. The real index needs to subtract the offset of 127. The range is (- 126 ~ 128)
• The last digit stores the decimal part to determine the precision of floating-point number. The larger the number that can be represented by decimal, the greater the accuracy and the more accurate the value

• The last digit of float is 23, 2 ^ 23 = 83886088388608 is a 7-digit decimal system. If the omitted integer digits are added, the maximum number of decimal digits can be 8-bit. However, it can absolutely guarantee that the effective number is a decimal number of about 7 bits; the last digit of double is 52, 2 ^ 52 = 45035996, 27370496, and the number of 16 digits, plus the integer digit 2 ^ 53, is also a 16 digit number, so it can absolutely guarantee that the significant bit accuracy is 15 digit decimal number
• 15.625 storage example:

• 15.625 to binary 1111.101
• Move 1111.101 three places to the right, leaving 1 place before the decimal point: 1.111101 * 2 ^ 3
• Bottom digit: because it must be 1 before the decimal point, it is only necessary to record the number of digits after the decimal point. At this time, the base number is 1111 01, and the low order zero padding is 111 1010 million 0000 0000 0000
• Index bit: the exponent is 3, plus 127 (minus 127 in inversion) to get 130, and the binary index bit is 1000 0010
• 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: Packaging class corresponding to basic type, difference

Basic types Packaging
boolean Boolean
short Short
byte Byte
int Integer
long Long
float Float
double Double
char Character
• Why is there a basic type in Java, where everything is an object? Because Java generates objects, it needs to be created and maintained on the heap, and then used by the stack reference. However, for simple small variables, it is too cumbersome to create and reuse them in the heap
• Why are there packaging classes

• Wrapper class encapsulates the basic type and makes it have the property of object. It can add attributes and methods to enrich the operation of basic type
• For generic programming, or using collection collections, you need to wrap classes. The generic type of HashMap cannot specify a base type because of ArrayList
• The difference is that the basic type can be declared and used directly. 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, and Boolean is true / false, and cannot be assigned null. The default initial value of the wrapper class is null
• Points to note: byte, int, short, long are directly assigned values (or valueof) such as integer x = value (value between – 128 and 127) refers to the object in the constant pool, and object comparison = = and equals are both true; when character declares value, it refers to constant pool object between 0 and 127 # 6: Automatic conversion of basic types

The automatic conversion rules are as follows

• Boolean type Boolean does not have an implicit conversion to another type (non auto encapsulated type)
• Automatic promotion of integer type

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

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

• Because the number of significant digits that floating-point type can save is limited, we need to consider the converted significant bit problem • Automatic promotion of floating point data

• There is a precision error problem in the conversion of float to double, and there is a problem of precision loss if double is forced to change to float # 7: Short S1 = 1; S1 = S1 + 1; is there a mistake?

• 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 add and get the result of int type. Finally, the result of int cannot be implicitly converted to short, and an error is reported
• S1 + = 1; there is an implicit forced conversion S1 + = 1 → S1 = (short) S1 + 1; there is no error in compilation

# 8: What are the possible problems with different basic type casts

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

# 9: Is float f = 3.4; correct?

In Java, floating-point numbers without suffix modification are of type double by default. Double type cannot be converted to float implicitly. An error will be reported when compiling

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

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

# 11: Floating point numbers and BigDecimal

• Floating point type uses binary storage, no matter float (7), double (15), its significant bit is limited, there is rounding error, and the precision is easy to be lost
• There is a serious problem of precision when decimal decimal is converted to floating point number. So whether it is possible to expand decimal decimal by N times to integer dimension for calculation, and retain its precision digits, which is BigDecimal
• BigDecimal is based on BigInteger to process the calculation. There is an int [] MAG inside BigInteger, which represents the principle of BigInteger for storing the original byte array of positive numbers
• Avoid floating-point type construction when constructing BigDecimal, which may cause precision problems. Try to use strings to create BigDecimal, or use the valueof method

``````BigDecimal data= new BigDecimal(0.1);
System.out.println("data:" + data);

result:
data:0.1000000000000000055511151231257827021181583404541015625``````
• BigDecimal will throw an arithmeticalexception exception when BigDecimal performs division operation without integer division, and the precision needs to be specified
• Specify the number of precision bits, and you need to specify the rounding mode

# 12: Can switch statements work on byte type variables, long type variables, and string type variables?

• Switch (expr) expr needs to be of type int, while byte, char, short will be automatically promoted to int, so it can be used on the switch keyword. Long, float, double cannot be converted to int automatically. An error will be reported when compiling
• String can’t be converted to int automatically, and it can’t be used in switch before 1.7. However, after 1.7, JDK supports string, and the String.hashCode Return an int type and use it again in case String.equals compare

``````//Syntax sugar decompiler
switch(s.hashCode()){
default;
break;
case 3556498:
if(s.equals("test")){
.....
}
break;
}``````

# 13: Can I assign a double value to a long variable without casting

No, because double is larger than long. No cast will compile and report errors. If you compile with the + = operator, no error is reported, but there is an implicit cast

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

• Do you really know how many bytes are occupied by Boolean type in Java?
• Why are floating-point numbers larger than int, long?

## Array of algorithms — sum of three numbers

Sum of three numbers difficultysecondary Here is an array of N integersnums, judgmentnumsAre there three elements a, B, C in a such that a + B + C = 0? Please find all triples that satisfy the condition and do not repeat.be careful:The answer cannot contain duplicate triples. Example:Given array nums = [- 1, 0, […]