Java Constants: Definition and Classification of Java Constants

A constant is a quantity whose value remains constant throughout the execution of a program. It should be noted here that constants and constant values are different concepts. Constant values are the concrete and intuitive manifestations of constants, and constants are formalized manifestations. Usually, constant values can be used directly or constants can be used in the program.

Let’s take a systematic look at the constant values in Java and the methods of defining constants.

Constant value

Constant values are also called literal constants, which are directly represented by data, so there are many data types, such as integers and strings. These constant values are described below.

Integer constant value

Java’s integer constant values mainly have the following three forms.

  • Decimal format: such as 54, -67, 0.

  • Octal number form: The representation of octal constants in Java starts with 0, such as 0125 represents the decimal number 85, and -013 represents the decimal number -11.

  • Hexadecimal number form: The representation of hexadecimal constants in Java starts with 0x or 0X, such as 0x100 represents the decimal number 256, and -0x16 represents the decimal number -22.

An integer (int) constant occupies 32 bits in memory by default, and is a value of integer type. When the required value exceeds 32 bits in length during operation, it can be expressed as a long integer (long) value. The long integer type needs to add L or 1 after the number, such as 697L, which means a long integer number, which occupies 64 bits in memory.

Real constant value

Java’s real constant values mainly have the following two forms.

  • Decimal format: It consists of numbers and decimal points, and must have a decimal point, such as 12.34, -98.0.

  • In scientific notation form: such as 1.75e5 or 32 & amp;E3, where e or E must be preceded by a number, and the number after e or E must be an integer.

Java real constants occupy 64 bits in memory by default and are double-precision (double) values. If you consider the need to save system resources at runtime, and the data value range during operation is not large and the operation precision is not too high, you can express it as a single-precision (float) value.

Single-precision values generally need to add F or f after the constant, such as 69.7f, which means a float-type real number, which occupies 32 bits in memory (depending on the version of the system).

Boolean constant value

Java’s Boolean constants have only two values, false (false) and true (true).

Character and string constant values

Java’s character constant value is a character enclosed in single quotes, such as ‘e’, E’. It should be noted that single quotes and double quotes cannot be mixed in Java string constant values. Double quotes are used to represent strings, like “11”, “d”, etc. are all strings representing a single character.

In addition to the above-mentioned character constant values, Java also allows the use of a special form of character constant values to represent some characters that are difficult to represent with general characters. This special form of characters is a sequence of characters at the beginning, called escape character.

Note: The single quotes and double quotes that represent characters and strings must be the symbols entered in the English input environment.

Table 1 lists the escape characters commonly used in Java and their meanings.

Table 1 Commonly used escape characters in Java

escape character

illustrate

\ddd

Character represented by 1~3 octal numbers

\uxxxx

Character represented by 1~4 hexadecimal numbers

single quote character

double quote character

\

double slash character

\r

carriage return

\

new line

\b

backspace

\t

horizontal tab

Define constants

A constant is different from a constant value, it can be used in a program as a symbol instead of a constant value, so it must be defined before use. Similar to variables, constants also need to be initialized, that is, they must be assigned an initial value while declaring the constant. Constants cannot be modified once initialized. Its declaration format is:

The Java language uses the final keyword to define a constant, and its syntax is as follows:

final dataType variableName = value

Among them, final is the keyword to define the constant, dataType indicates the data type of the constant, variableName is the name of the variable, and value is the initial value.

The final keyword means final, it can modify many elements, and the modified variable becomes a constant. For example, the following statement declares constants using the final keyword.

publicclass HelloWorld{
// static constant
public static final double PI =3.14;
// Declare member constants
finalint y = 10;

public static void main(String[] args){
// Declare local constants
finaldouble x = 3.3;
}
}

There are three types of constants: static constants, member constants, and local constants.

The third line of the code is to declare a static constant, which is decorated with public static before final. The scope of the constant modified by public static is global, and it can be accessed without creating an object, and the access form outside the class is HelloWorld.PI. This constant is used a lot in programming.

Line 5 of the code declares a member constant, which has a scope similar to that of a member variable, but cannot be modified. Line 9 of the code declares a local constant, whose scope is similar to that of a local variable, but cannot be modified.

When defining constants, you need to pay attention to the following:

  • The constant needs to be initialized when it is defined.

  • The final keyword can be used not only to modify constants of basic data types, but also to modify object references or methods.

  • In order to distinguish from variables, constants are generally named with uppercase characters.

After the constant is set, it is generally not allowed to change it, and an error will be prompted if its value is changed. For example, if the constant AGE is defined and given an initial value in the figure below, if the value of AGE is changed, an error of “cannot overlap assignment” will be prompted at compile time.