Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

introduce

There are eight data structures in Java: byte, short, int, Long, float, Double, char, and Boolean.

Basic data types Storage size The minimum value The maximum The default value
byte eight – 128 (
2 7 – 2 ^ 7
)
127 (
2 7 1 2 ^ 7-1
)
0
short 16 – 32768 (
2 15 – 2 ^ {15}
)
32767 (
2 15 1 2 ^ {15} – 1
)
0
int 32 – – 2147483648 (
2 31 – 2 ^ {31}
)
2147483647 (
2 31 1 2 ^ {31} – 1
)
0
long A 64 – bit – 9223372036854775808 (
2 63 – 2 ^ {63}
)
9223372036854775807 (
2 63 1 2 ^ {63} – 1
)
0L
float 32 – 0.0 f
double A 64 – bit 0.0 d
char 16 \u0000 (i.e. 0) \ UFFFF (65535) \u0000
boolean 1 a false true false

Conversions between basic data types

Conversion between numeric types

Conversions between numeric types are governed by the following principles: when broadening types, you do not need to explicitly declare conversions; When you narrow a type, you need to display the declared type conversion. Broadening is a conversion from a small range to a large range, such as from int to long. Int itself has a smaller range than long, so it can be converted implicitly. The reverse is the narrowing type, which typically loses precision if it exceeds the value range of the narrow type to which it is converting.

Conversion between numeric and character types

Numeric types can be converted directly to character types, and because character types have only 16 bits of storage, only the lower 16 bits are used. In the case of floating-point, only the integer part is converted to a character type.

char a = 97;
// the value of a is 'a'
Copy the code

Conversion from character type to numeric type can also be performed directly, converting the unicode of a character to a specified numeric type. However, if the value is outside the range of the data type to be converted, an explicit conversion declaration is required.

int a = 'a'
// The value of a is 97, the ASCII code of character A
Copy the code

A wrapper class for the base data type

Java provides a wrapper class for each base data type

Byte → byte, short → short, int → Integer, long → long, float → float, double → double, char → Character, Boolean → Boolean

This is done to solve the problem that the base data types are not object-oriented.

There is no need to remember the range of values for each of the basic data types in the table above; their corresponding values can be found in the corresponding wrapper class, as shown in the following

System.out.println("Number of bits in int:" + Integer.SIZE);
System.out.println("Minimum value of int:" + Integer.MIN_VALUE);
System.out.println("Maximum value of int:" + Integer.MAX_VALUE);
Copy the code

The results for

Minimum value of int: -2147483648 Maximum value of int: 2147483647Copy the code

Integer.min_value and so on are often seen in algorithm problems.

conclusion

In fact, the basic data types are similar in all languages. The most important thing is to master the mutual conversion of data types and understand the difference in the value range of each data type.