JAVA basics - basic data types



Type Classification Overview

Divided into 4 categories: integer , character type , floating point type , Boolean type .
If you insist, the character type can also be used as a 2-byte unsigned integer, so there are 3 types in total.
Java is a strongly typed language. Each type has clear and precise regulations. The regulations clearly define the field width and value range, which are unified on any platform.
The details of each type are explained below.



integer

Integer type width and value range

Java's integer type is different from C++, its width and value range are fixed, and java does not have an unsigned type unsigned.
As shown in the table below:

type width scope
byte 8 bits -128 ~ 127
short 16 bits - 2 15 2^{15} 215 ~ 2 15 2^{15} 215 - 1
int 32 bit - 2 31 2^{31} 231 ~ 2 31 2^{31} 231 - 1
long 64 bit - 2 63 2^{63} 263 ~ 2 63 2^{63} 263 - 1

integer literal

An integer literal is a quantity whose literal value has the same meaning. Such as 312, 4303943894L, etc.

Integer Literal Representation

Integer literals have 4 representations according to different bases, which are distinguished by different prefixes. The literals starting with 0B or 0b are binary literals, the literals starting with 0 are octal literals, the literals without a prefix are decimal literals, and the literals starting with 0x or 0X are hexadecimal literals.
Note that representation and storage in memory are two different things, and all integers in memory are stored in two's complement form .

type of integer literal

Java's integer literals default to the int type, but there are two parts to understand:
1. If the integer literals are assigned to the byte and short types, and the value of the literal is within the range of the corresponding type, then the literal It will be automatically converted to the corresponding type; if the value of the literal exceeds the range of the type, an error will be reported.
For example, byte b1 = 127; 127 is within the range of byte, and 127 is assigned to a byte variable, then the literal value of 127 will be converted to byte type.
But if it is byte b1 = 128; then 128 is not within the byte range, but it needs to be directly assigned to a variable of byte type, then an error will be reported directly.
2. For literals beyond the range of int, you want to assign a value to the long type, such as adding the suffix L or l.
For example, 3_000_000_000, which exceeds the maximum value of int, the system will not default 3 billion as long type.
For the code long l1 = 3_000_000_000; the system will directly report an error.
If you want to successfully assign to a variable of type long, you must add the suffix L or l. The code should be changed to: long l1 = 3_000_000_000L;

The summary is: small integers will be automatically converted according to the assigned variable type, and large integers must be suffixed with L.



character type

The character type maps characters to corresponding binary numbers by means of encoding and stores them in the computer.
The encoding method of java is Unicode encoding, which is 2 bytes, a total of 16 bits, so the width of the char type is 2 bytes, which can represent up to 65536 characters.

character type symbol

The char type can also be treated as a 2-byte integer. It is an unsigned type and can represent integers in the range of 0~65535.

character literal

There are three representations of char literals, which are similar to C++.
1. The method of enclosing characters in single quotation marks, such as 'A', 'b', etc., the char type variable stores the binary code of the corresponding character.

2. Escape characters represent characters that cannot be output from the keyboard or characters with special uses.
Please refer to the table below for escape characters that need to be remembered:

character name C++ code
line break \n
horizontal tab \t
backslash \\
apostrophe \’
Double quotes \"

3. The character value represented by Unicode code, the format is: '\uxxxx', where xxxx is 4 hexadecimal numbers, which represent the corresponding characters in Unicode.
Such as '\u9997' this format.



floating point type

Java's floating-point types are divided into two categories: float and double . Used to represent decimals.

Precision and Field Widths for Floating-Point Types

Both floating-point types have fixed precision and field lengths.
Float has a total of 32 bits, 1 bit is a sign bit, 8 bits are exponent bits, and the remaining 23 bits are decimal bits.
The double type has a total of 64 bits, 1 bit is the sign bit, 11 bits are the exponent bits, and the remaining 52 bits are the decimal bits.
Floating-point numbers are internally stored in binary and divided into two parts, and the exponent and mantissa are stored separately in binary. Powers of exponent bits are not powers of decimal, but powers of binary.
Due to limited precision, errors may occur when the mantissa of the decimal is particularly long.

floating point literal

Floating point literals are numbers with a decimal point. You can assign values ​​to variables of floating point type.

Representation of floating-point literals

There are 2 representations:
1. Decimal form of decimal system.
That is, the most common and most intuitive representation of mathematical form. Such as 0.12 and so on.
2.E Notation
This notation is similar to scientific notation in mathematics.
Divided into 2 parts, the left represents the mantissa, and the right represents the exponent. The power of the exponent is in decimal, which is in line with the habit of programmers.
For example, 3.2E3 is equivalent to 320.0.

Types of floating-point literals

Floating-point literals are of double type by default. If you need to convert to float type, for example, add the suffix F or f.

3 special values ​​for floating point literals

There are 3 special values, namely: positive infinity, negative infinity and non-number.
Positive Infinity: Any positive floating-point number divided by 0 yields positive infinity.
Negative infinity: Divide any negative floating point number by 0 to get negative infinity.
Positive infinity and negative infinity are constants defined by Java's Double class and float class. Positive infinity is Double.POSITIVE_INFINITY and Float.POSITIVE_INFINITY; negative infinity is Double.NEGATIVE_INFINITY and Float.NEGATIVE_INFINITY.
Note that any positive infinity is equal (it doesn't matter whether it's in the Double class or in the Float class).
Any negative infinity is also equal.
Also note that positive infinity and negative infinity are special values ​​of floating-point numbers, which must be obtained by dividing a floating-point number by 0. Dividing an integer by 0 will directly report an exception.

Not a number: Divide 0.0 by 0.0 or square root a negative number to get a not number. Non-numbers are represented as Double.NaN or Float.NaN.
It is worth noting that any non-numbers are not equal. For example, Double.NaN is not even equal to Double.NaN.



Boolean type

There is only one type of boolean type: boolean type (note that it is different from bool in C++). Used to represent logical true and false.
Unlike C++, the values ​​of the Boolean type are only true and false, and have no correspondence with non-zero integers and zero. You cannot use 0 to represent false and non-zero to represent true.

Boolean field length

JAVA does not specify exactly the length of the Boolean type. Although only 1 bit is required, it is often determined according to the smallest memory unit allowed by the current system. Many systems use 1 byte.



var type

This is a dynamic type. The specific type of a variable of type var is determined by the value on the right side of the initialization.
I don't think the var type is of great value in java, because java is a strongly typed language, each type has precise regulations, and any platform is unified.
The dynamic type of var is essentially a strong type, but the way to determine the type is determined by the initial value. Once determined, the type of the variable is determined.
For example, var num = 5; The literal value of 5 is of type int by default, so the num variable is of type int.
Another example is var a1 = 3.14; 3.14 is of type double by default, so the variable a1 is of type double.
A variable of type var must be initialized, otherwise the compiler cannot determine the type of the variable.

var can be harmful if not used carefully!
If the initialized value of a variable of type var comes from the return value of a certain method, it is difficult for the programmer to determine the true type of the variable at a glance, thereby reducing readability.

Guess you like

Origin blog.csdn.net/qq_983030560/article/details/128945873