Constants in Java SE 038 programs and when to use constants

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Constants in Java SE 038 programs and when to use constants

1. Constants in the program and in what occasions to use constants

(1) The naming rules for constants in java: the letters of all words are capitalized, if there are multiple words, use underscores to connect them.

例如:public static final int AGE_OF_PERSON = 20;

(2) Why are static and final often used together?

Reason: If static is not added, then this value is only final, and every object has this value. The value in each object cannot be changed. If static is added, no matter how many objects are generated, there is only one constant value, and it cannot be changed. Shared by multiple objects, that is, read it together. For 10 objects, one is to generate 10 constants, and the other is to generate 1 constant. Which is better? One is definitely better. Therefore, static is usually added to the constant to modify it.

(3) What is the role of constants?

(4) The static keyword is usually added when declaring final constants in Java, so that each instance of the object will access a unique constant value.

In this way, each instance of the object will access a unique constant value.

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501231