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

What do Java constants and constant values mean

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you what Java constants and constant values mean. The content is simple and clear. I hope it can help you solve your doubts. Let me take you to study and learn what Java constants and constant values mean.

1 constant definition

There is a large amount of data in the program to represent the state of the program, some of which can not be changed while the program is running. These data are called constants in the program.

2 constant syntax naming

The syntax format of the Java constant is as follows:

Final data type constant name = value

The sample code is as follows:

/ / declare a double-precision constant PI and assign final double PI = 3.14

The naming rules for Java constants are as follows:

Rule 1: the letters of all words are capitalized, if there are more than one word, then use an underlined link.

/ / declare integer constant age AGE_OF_PERSONpublic static final int AGE_OF_PERSON = 18

Rule 2: do not allow any magic values (that is, undefined constants) to appear directly in the code.

/ / declare CONTENT string type, age does not define final String CONTENT = "name" + age

Long or Long initial assignment must use uppercase L, not lowercase l, lowercase is easy to be confused with the number 1, resulting in misunderstanding.

/ / Life length integer COUNT, is it the number 91 or the long type 9?final Long COUNT = 9lter3 constant value

Constant values, also known as literal constants, are represented directly by data, so there are many data types, such as integers and strings.

It should be noted here that constant and constant value are different concepts, constant value is the concrete and intuitive expression of constant, and constant is the formal expression. Usually in the program, you can use either constant values directly or constants.

3.1 Integer constant value

The integer (int) constant occupies 32 bits in memory by default and is a value of integer type, which can be represented as a long integer (long) value when the required value during the operation exceeds the 32-bit length. The long integer type is followed by an L or 1, such as 678L, which represents a long integer, which occupies 64 bits in memory.

3.2 floating point constant value

The Java floating-point constant occupies 64 bits in memory by default and is a value with double precision (double). If you consider the need to save system resources at run time, and the range of data values during operation is not large and the accuracy of operation is not too high, it can be expressed as a single precision (float) value.

3.3 Boolean constant valu

Java's Boolean constant has only two values, false (false) and true (true).

3.4 character constant value

The character-type constant value of Java is a character enclosed in single quotation marks, such as'a','A'.

3.5 string constant value

Double quotation marks are used to represent strings, such as "11", "d", etc. are strings that represent a single character.

4 programmers to gain insight into 4.1 points for attention

When a constant is set, it is generally not allowed to change it, and changing its value will prompt an error.

Public static void main (String [] args) {/ / constant AGE assignment final int AGE = 18; / / modify constant AGE, program error AGE = 23;} 4.2 constant action

Constants play two main roles in the process of running a program:

1. Represents a constant, which is convenient for program modification (for example, the value of pi)

Final double PI = 3.14

2. Enhance the readability of the program (for example, the constants UP and DOWN stand for up and down respectively, and their values are 1 and 2, respectively)

Final int UP = 1 final + int DOWN = 2 * * 4.3 constant declaration position

In a project, there are generally a lot of constants, which are often due to high access efficiency. They are usually declared in the following locations:

1. Put it into Interface, because the fields declared in Java interface will be automatically appended with the modifier of static final at compile time, that is, declared as constant.

2. Put it into a file, such as a Properties file.

3. Put it in Class and use final String.

What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.

The above is about "what is the meaning of Java constant and constant value". If this article is helpful to you and think it is well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.

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