What are Java constants?
JAVA constants are fixed values in the program and data that cannot be changed. For example, the number 1, the character "a", the floating point number 3.2, etc. In Java, constants include integer constants, floating-point constants, Boolean constants, character constants, etc.
Let’s take a look at these constants in java.
1. Integer constants
Integer constants are integer type data, with four specific representation forms: binary, octal, decimal and hexadecimal. as follows.
Binary: A sequence of numbers consisting of the digits 0 and 1. In JDK7.0, literal values are allowed to represent binary numbers, which must start with 0b or 0B in order to distinguish them from decimal numbers, such as 0b01101100, 0B10110101.
Octal: A sequence of numbers starting with 0 and followed by integers in the range of 0 to 7 (including 0 and 7), such as 0342.
Decimal: A sequence of numbers consisting of integers in the range of 0 to 9 (including 0 and 9). Such as 198.
Hexadecimal: A sequence of numbers starting with 0x or 0X and followed by 0~9, A~F (including 0 and 9, A and F), such as 0x25AF.
It should be noted that in order to indicate different bases in the program, the data has specific identifiers. Octal must start with 0, such as 0711, 0123; hexadecimal must start with 0x or 0X. Such as 0xaf3, 0Xff; when the integer is expressed in decimal, the first digit cannot be 0, except 0 itself. For example, 127 in decimal is represented as 011111 in binary, 017 in octal, and 0x7F or 0X7F in hexadecimal.
2. Floating-point constants
Floating-point constants are decimals used in mathematics, divided into feet There are two types of single-precision floating-point numbers and double-precision floating-point numbers. Among them, single-precision floating-point numbers end with F or f, while double-precision floating-point numbers end with D or d. Of course, when using floating point numbers, you can also add no suffix at the end. In this case, the virtual machine will default to double precision floating point numbers. Floating point constants can also be represented in exponential form. Specific examples are as follows:
2e3f 3.6d 0f 3.84d 5.022e+23f
3. Character constants
Character constants are used to represent a character. A character constant should use a pair of single quotes in English half-width format'' It can be English letters, numbers, punctuation marks, and special characters represented by escape sequences. Specific examples are as follows:
‘a’ ‘1’ ‘&’ ‘\r’ ‘\u0000’
In the above example, '\u00' represents a blank character, that is, there are no characters between single quotes. The reason why it can be represented like this is because Java uses the Unicode character set. Unicode characters start with \u, and the corresponding value of blank characters in the Unicode code table is '\u0000'.
4. String constants
String constants are used to represent a series of consecutive characters. A string constant should use a pair of double quotes in English half-width format." ”, specific examples are as follows:
“HelloWorld" “123" "We come \n XXX" "”
A string can contain one character or multiple characters, or it can not include any characters, that is, the length is zero.
5. Boolean constants
Boolean constants are the two values of Boolean type, true and false. This constant is used to distinguish the true or false of a thing.
6. Null constant
The null constant has only one value, null, which means that the reference to the object is empty.
Recommended learning:Java video tutorial
The above is the detailed content of What are java constants?. For more information, please follow other related articles on the PHP Chinese website!