Boolean type
Boolean consumes one byte in memory.
When the Java compiler compiles the Java source code into bytecode, it uses an int or byte to represent the Boolean. In a Java virtual machine, the integer zero is used to denote false, and any nonzero integer is used to represent true.
Java Virtual machine This underlying processing is transparent to the Java virtual machine, in the Java source program Boolean variables can only be true or false
BYTE, short, int, and long integer types
byte, short, int, long are integer types, and are signed integers occupy 1, 2, 4, 8 bytes, respectively.
The value range is 127-127, 2 of the 15 square to 2 of 15 square minus one, 2 31 to 2 of the 31 square minus one, 2 of 63 to 2 square minus one
Byte takes a byte value range of 128-127 in memory. When a variable is defined. For example, the month value is 1 to 12. As a result, defining the month variable that represents months as a byte type saves memory space.
In practical applications, month is typically defined as an int type, because this simplifies the operation of the type conversion when the data is being manipulated.
BYTE month = 1;
Month = month+2;//Compilation error requires a strong turn
month = (byte) (month+2); Compile correctly
The default type of integer constant value in Java is type int
char character type
Char is a character type that occupies two bytes , the Java language uses Unicode character encoding for characters.
Char is stored in characters, whether English or Chinese, with a fixed occupancy of 2 bytes to store Unicode characters. The range is in 0-65536.
Both Utf-8 and utf-16 are processing Unicode encodings, and their encoding rules are not the same
UTF-16 has a high coding efficiency, which is simpler to convert from byte to character, and better for string manipulation. It is suitable for use between local disk and memory, and can be used to quickly switch characters and bytes directly.
such as Java memory encoding is used UTF-16 encoding. However, it is not suitable for the direct transmission of the network utf-16 in sequential encoding, the encoding value of a single character can not be verified. If the middle one character code value is damaged, all subsequent code values are affected.
Because the network transmission is easy to damage the byte stream, once the byte stream is damaged it is difficult to recover.
Utf-8 is more suitable for the network transmission of characters using a single byte of storage, the Chinese characters using three bytes , the other single-byte damage will not affect the other characters, in the coding efficiency between GBK and utf-16, coding efficiency and security of the balance
So Utf-8 is the ideal Chinese coding method
The Blue font section above looks paradoxical (declare kanji with char). The Unicode and UTF encodings are explained below
Unicode encoding is compiled by the International Unicode Association, which contains characters from all languages in the world and is a cross-platform character encoding USC (Universal Character set) refers to a universal character set that uses Unicode encoding. There are two encoding schemes for Unicode.
Encoded with two bytes (16 bits), the character set using this encoding scheme is called UCS-2. The Java language uses a two-byte encoding scheme
Encoded in four-byte (32-bit) (in fact only 31 bits, the highest bit must be 0), the character set using this encoding is called UCS-4.
UTF encoding: Some operating systems do not fully support 16-bit or 32-bit Unicode encoding, UTF (UCS transformation Format) encoding can convert Unicode encoding to the encoding supported by the operating system, and common UTF encodings have Utf-8,utf-7,utf-16
Summary: The seemingly contradictory part of the blue font above is because UTF encoding transforms and optimizes Unicode (UCS)
float and double floating-point types (with decimals)
Float: Four bytes, total 32 bits, called single-precision floating-point number
Double: eight bytes, a total of 64 bits, called double-precision floating-point numbers
Floating-point constant values in Java are by default double type
Note: The floating-point types provided by Java are not suitable for precise operations
Public Static void throws Exception { double money = 1; double price = 0.1; System.out.println (money-price*9); // Print out 0.09999999999999998 (this should be 0.1) }
Workaround: The Java.math.BigDecimal class can meet the user's accuracy note when constructing the BigDecimal class, the parameters in the constructor method are not recommended when floating-point types need to be converted to string types
Basic Data type Conversions
The above four categories of eight basic data types. Except for the Boolean type. Integer floating-point and character data can be converted in a mixed form.
automatic type conversion from low to high (up)
Forced type conversions are required from high to low. (Downward transition)
From low to high byte, char, short, int, long, float, double
float f = 3.14; Compile error, cannot assign double type directly to float type variable requires coercion type conversion
In your application, forcing type conversions can lead to data overflow or reduced precision, and you should avoid using forced type conversions.
Casting an int to a byte type in the following code causes the data to overflow.
int a = 256;
BYTE AA = (byte) A;
int b = 222;
byte BB = (byte) b;
SYSTEM.OUT.PRINTLN (AA); Printing results 0
System.out.println (BB); Print Result-34
Java four class eight basic data types