Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition and classification of Java constant

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the definition and classification of Java constants. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the definition and classification of Java constants.

1. The definition of constant:

The broad concept of "constant" is: a quantity that does not change (for example, a quantity that will not be modified by the program while the computer program is running; a quantity in a mathematical function, such as the radius and diameter of each specific circle; the acceleration of gravity near the ground in physics; the value of the speed of light in a vacuum In other words, constant, although it serves hardware, software and programming language in computer technology, is not a concept introduced specifically for hardware, software and programming language. Constants can be divided into different types, such as: 25, 0,-8 are integer constants, 6.8 and-7.89 are real constants, and'a 'and' b 'are character constants. Constants can generally be judged from their literal form. This constant is called a literal constant or a direct constant.

Second, the classification of constant:

1. Integer constant

Integer constants are integer data, which are represented in four forms: binary, octal, decimal and hexadecimal.

Binary: a sequence of numbers consisting of the numbers 0 and 1. In JDK7.0, literal values are allowed to represent binary numbers, starting with 0b or 0B in order to distinguish them from decimal systems, such as 0b01101100 and 0B10110101.

Octal: a sequence of numbers, such as 0342, that begins with 0 and is followed by integers in the range 0 to 7 (inclusive).

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, such as 0x25AF, that begins with 0x or 0X and is followed by 0x 9, A F (including 0 and 9, An and F).

It should be noted that in the program, in order to identify different decimal systems, the data has a specific identification, octal must start with 0, such as 0711 0123; hexadecimal must start with 0x or 0X, such as 0xaf3, 0Xff. when integers are represented in decimal, the first place cannot be 0B0 itself. For example, decimal 127 is represented as 011111 octal in binary, 017 in octal and 0x7F or 0X7F in hexadecimal.

2. Floating point constant

Floating point constant is the decimal used in mathematics, which can be divided into two types: foat single precision floating point number and double double precision floating point number. Where single-precision floating-point numbers end with F or f, while double-precision floating-point numbers end with D or d. Of course, you can also use floating-point numbers without any suffix at the end, and the virtual machine defaults to double double-precision floating-point numbers. Floating point constants can also be expressed in exponential form. Specific examples are as follows:

2e3f 3.6d 0f 3.84d 5.022e+23f

3. Character constant

Character constants are used to represent a character. A character constant is quoted by 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 white space character, that is, there are no characters between single quotation marks. The reason for this is that Java uses the Unicode character set, the Unicode character begins with\ u, and the value of the white space character in the Unicode code table is'\ u0000'.

4. String constant

String constants are used to represent a series of consecutive characters, and a string constant is enclosed by a pair of double quotes in English half-width format, as shown in the following example:

"HelloWorld"123"We come\ nXXX"

A string can contain one or more characters, or it can not include any characters, that is, zero in length.

5. Boolean constant

Boolean constants are Boolean values true and false, which are used to distinguish the true from the false of a thing.

6. Null constant

The null constant has only one value, null, indicating that the reference to the object is empty.

Third, the function of constant:

1. Constants replace ambiguous numbers or strings with clear names that are easy to understand, making the program easier to read.

2. Constants make the program easier to modify. For example, in a C # program, there is a SalesTax constant that has a value of 6%. If the sales tax rate changes in the future, assign the new value to this constant, you can change all the tax calculation results without having to look up the entire program and modify each item with a tax rate of 0.06.

3. Constants are easier to avoid program errors. If a value is assigned to a constant in the program and the constant already has a value, the compiler reports an error.

Thank you for your reading, the above is the content of "the definition and classification of Java constant". After the study of this article, I believe you have a deeper understanding of the definition and classification of Java constant, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report