Home  >  Article  >  Java  >  What is the difference between java variables and constants

What is the difference between java variables and constants

王林
王林Original
2020-05-14 13:33:347517browse

What is the difference between java variables and constants

The difference between Java variables and constants is:

1. In the Java coding standards, constant names are required to be in uppercase letters, and variable names are required to be in uppercase letters. Lowercase;

2. Constants can only be assigned once, and variables can be assigned multiple times;

3. Constants cannot be changed while the program is running, but variables can be changed.

(Video tutorial recommendation: java video)

Let’s take a look at the detailed introduction of constants and variables:

1. Constants

1. Definition: Constants represent values ​​that cannot be changed during program running.

2. Syntax format:

[访问修饰符] final 数据类型 常量名称 = 值

The keyword final is indispensable, the constant name must be in capital letters, and the content in square brackets is optional.

3. Features

(1) There is the keyword final

(2) In the Java coding specification, constant names must be capitalized

(3 ) must be declared before use. A value can be assigned at declaration time or at any time before use, but it can only be assigned once.

Note: Global constants do not need to be manually assigned, the system will initialize the values ​​of these global constants. Local constants must be assigned values, otherwise a compilation error will be reported when used.

2. Variables

1. Definition

Variables are amounts whose values ​​can change during program running.

2. Grammar format

[访问修饰符] 数据类型 变量名[=初始值]

The content in brackets is optional.

3. Features

(1) In the Java coding standards, variable names must be lowercase

(2) They must be declared first and then used. Values ​​can be assigned at declaration time or at any time before use. Can be assigned an unlimited number of times.

Note: Global variables do not need to be manually assigned, the system will initialize the values ​​of these global variables. Local variables must be assigned values, otherwise a compilation error will be reported when used.

Recommended tutorial: java entry program

The above is the detailed content of What is the difference between java variables and constants. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn