Like other languages, Java also has the concept of constants. Constants, as the name implies, are constant values that cannot be modified. Let's take a look at the various constants in Java.
Constant classification
1. Integer
Java's integer constant values mainly have the following three forms.
Decimal number form: such as 156, -32, 0.
Octal number form: The representation of octal constants in Java starts with 0, for example, 015 represents the decimal number 13, and -013 represents the decimal number -11.
Hexadecimal number format: The representation of hexadecimal constants in Java starts with 0x or 0X. For example, 0x100 represents the decimal number 256, and -0x16 represents the decimal number -22.
Integer (int) constants occupy 32 bits in memory by default and are values of integer type. When the value required during the operation exceeds 32 bits, it can be expressed as a long integer (long) value. In some textbooks or questions, sometimes long integer types require L after the number, such as 235L, which represents a long integer number, which occupies 64 bits in memory.
2. Real numbers
Java's real constant values mainly have the following two forms.
Decimal number form: It consists of numbers and decimal points, and there must be a decimal point, such as 23.4, -110.0.
Scientific notation form: such as 1.68e10 or 3&E5, where there must be a number before e or E, and the number after e or E must be an integer. Java real constants occupy 64 bits in memory by default and are double values. If you consider the need to save running system resources, and the range of data values during operation is not large and the operation accuracy is not high, you can express it as a single-precision (float) value. Single-precision values generally require F or f after the constant, such as 69.7f, which represents a float real number, which occupies 32 bits in memory (depending on the version of the system).
3. Boolean type
Boolean constants in Java have only two values, true and false.
4. Character and string constant values
Java's character constant value is a character enclosed in single quotes, such as 'a', 'A'. It should be noted that single quotes and double quotes in Java string constant values cannot be mixed. Double quotes are used to represent strings, such as 123, dotcpp, etc., which represent single character strings.
5. Escape characters
In addition to the above-mentioned forms of character constant values, Java also uses a special form of character constant values to represent some characters that are difficult to display using characters. This special form of character is a character sequence starting with , which is called a conversion. meaning characters.
The common escape character table is as follows:
It is enough to understand the content of this section. There is no need to memorize it by rote. After understanding the concept of constants, the values during subsequent coding processes such as assignment, addition, subtraction, multiplication and division are all constants. As the amount of our coding increases, it will naturally There is a deeper understanding of constants, so you don't have to worry too much.