What is the difference between constant variables and final variables in java?

笑着哭i 提交于 2020-07-02 08:37:59

问题


Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it.


回答1:


Constant is the concept, the property of the variable.

final is the java keyword to declare a constant variable.


As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness.

Quoting the specification, anyway, we can read

A variable of primitive type [...], that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.

I suppose, hence, that we can accept (and consider correct) this binomial for our purpose.




回答2:


Constant is not a keyword in Java.

It is a concept to make any variable constant. For this we use final keyword in Java so that after initializing the variable with final keyword , no one can reassign the value of that variable.




回答3:


There are several values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. These values remain constant. When writing a program it makes sense to represent them in the same way - as values that will not be modified once they have been assigned to a variable. These variables are known as constants.

Declaring a Variable as a Constant

In declaring variables I showed that it’s easy to assign a value to a int variable:

int hoursInADay = 24;

We know this value is never going to change in the real world so we make sure it doesn’t in the program. This is done by adding the keyword modifier final:

final int HOURS_IN_A_DAY = 24;

In addition to the final keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your code.

If we now try and change the value of HOURS_IN_A_DAY:

final int HOURS_IN_A_DAY = 24; 
HOURS_IN_A_DAY = 36;

we will get the following error from the compiler:

cannot assign a value to final variable HOURS_IN_A_DAY

The same goes for any of the other primitive data type variables. To make them into constants just add the final keyword to their declaration.

Where to Declare Constants

As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:

public class Hours {
   public static final int HOURS_IN_A_DAY = 24;
}



回答4:


Make variable "final" means we cannot reassign a value to this variable (ie we can use variable = Something once and only once.

So for primitive we can say final variable are constante.

But final variable can be non constant. for exemple

final Stringbuffer string = new StringBuffer("not"); 
string.append(" constant");
System.out.println(string); 

will print the string "not constant". Here "string" variable is final but not constant. i hope this will help



来源:https://stackoverflow.com/questions/30545594/what-is-the-difference-between-constant-variables-and-final-variables-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!