java中的常量变量和最终变量有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30545594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:46:59  来源:igfitidea点击:

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

javavariablesconstantsfinal

提问by Swaram Jagtap

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

请帮助我理解finalJava中常量变量和变量之间的区别。我有点困惑。

采纳答案by Luigi Cortese

Constantis the concept, the property of the variable.

常量是概念,是变量的属性。

finalis the java keyword to declare a constant variable.

final是用于声明常量变量的 java 关键字。



As other people pointed out, from a semantic/linguistic point of view the expression constant variableis 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.

原始类型 [...] 的变量是 final 并使用编译时常量表达式(第 15.28 节)初始化,称为常量变量

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

因此,我想,为了我们的目的,我们可以接受(并认为是正确的)这个二项式。

回答by Saket Mittal

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.

现实世界中有几个永远不会改变的价值观。正方形总是有四个边,PI 到小数点后三位总是 3.142,一天总是有 24 小时。这些值保持不变。在编写程序时,以相同的方式表示它们是有意义的 - 将它们分配给变量后就不会修改的值。这些变量称为常量。

Declaring a Variable as a Constant

将变量声明为常量

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

在声明变量时,我展示了为 int 变量赋值很容易:

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

final int HOURS_IN_A_DAY = 24;

In addition to the finalkeyword 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.

除了final关键字之外,您应该已经注意到,根据标准 Java 命名约定,变量名的大小写已更改为大写。这使得更容易发现代码中哪些变量是常量。

If we now try and change the value of HOURS_IN_A_DAY:

如果我们现在尝试更改 的值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

无法为最终变量 HOURS_IN_A_DAY 赋值

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

任何其他原始数据类型变量也是如此。要将它们变成常量,只需将final关键字添加到它们的声明中即可。

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;
}

回答by Amit Das

Constantis not a keyword in Java.

Constant不是 Java 中的关键字。

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

使任何变量保持不变是一个概念。为此,我们final在 Java 中使用关键字,以便在使用 final 关键字初始化变量后,没有人可以重新分配该变量的值。

回答by soung

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

使变量“ final” 表示我们不能为这个变量重新赋值(即我们只能使用variable = Something一次,并且只能使用一次。

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

将打印字符串“not constant”。这里的“字符串”变量是最终的但不是常量。我希望这个能帮上忙