java 如何处理最终字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4704320/
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-10-30 07:36:58  来源:igfitidea点击:

How to deal with Final Strings?

javastringtypes

提问by Rachel

Is there any advantage of making String as finalor can we make String as final?, my understanding is that as String is immutable, there is no point of making it final, is this correct or they are situation where one would want to make String as Final?

制作String as final或我们可以制作有什么优势String as final吗?我的理解是,由于 String 是不可变的,因此将其设为最终没有意义,这是正确的还是人们想要制作的情况String as Final

Code:

代码:

private final String a = "test";

or 

private String b = "test";

回答by Feras Odeh

finalmeans that the reference can never change. Stringimmutability means something different; it means when a Stringis created (value, not reference, i.e: "text"), it can't be changed.

final意味着引用永远不会改变。String不变性意味着不同的东西;这意味着当 aString被创建时(值,不是引用,即:“文本”),它不能被改变。

For example:

例如:

String x = "Strings Are ";
String s = x;

Now s and x both reference the same String. However:

现在 s 和 x 都引用相同的String。然而:

x += " Immutable Objects!";
System.out.println("x = " + x);
System.out.println("s = " + s);

This will print:

这将打印:

x = Strings Are Immutable Objects
s = Strings Are

This proves that any Stringcreated cannot be changed, and when any change does happen, a new Stringgets created.

这证明了任何String创建的都不能改变,当任何改变发生时,一个新的String被创建。

Now, for final, if we declare x as finaland try to change its value, we'll get an exception:

现在,对于final,如果我们将 x 声明为final并尝试更改它的值,我们将得到一个异常:

final String x = "Strings Are ";
x += " Immutable Objects!";

Here is the exception:

这是例外:

java.lang.RuntimeException: Uncompilable source code - cannot assign a value to final variable x

回答by david van brink

It means that that variablecan never even point to a different string.

这意味着该变量甚至不能指向不同的字符串。

(And yes, a String object is immutable... but a variable could point to a different instance of String. Unless it's final.)

(是的,一个 String 对象是不可变的......但是一个变量可以指向一个不同的 String 实例。除非它是最终的。)

(edit -- in your example, you didnt name your variable...

(编辑 - 在你的例子中,你没有命名你的变量......

final String x = "test";

)

)

回答by helloworld922

final means that the reference cannot change, not the value. Regardless of the final, Strings are always immutable.

final 意味着引用不能改变,而不是值。无论最终结果如何,字符串始终是不可变的。

final String a = "test";
String b = "hello";

a = "world"; // error, a can't change reference to the String object "world"
b = "two"; // ok, b points to a different String object "two"

回答by fastcodejava

finalmean variable cannot be changed.

final均值变量不能改变。

final String s = "abcd";
// later in the code
s = "efgh";  // This will not compile

Immutable means there is method on Stringwhich can change the contents in it, e.g. setter, append, etc.

不可变的装置有方法上String,其可以改变它的内容,例如setterappend

回答by Costis Aivalis

All fine here, but there are two more ways to "deal with Final Strings", as you put it, but in terms of 'where to place them':

在这里一切都很好,但还有两种方法可以“处理最终字符串”,正如您所说,但就“放置它们的位置”而言:

  1. Create an interface that includes final static variables (and final Strings) and implement it wherever you need them, and
  2. Create a class for constants and make a static import.
  1. 创建一个包含最终静态变量(和最终字符串)的接口,并在您需要的任何地方实现它,以及
  2. 为常量创建一个类并进行静态导入。

Way 1, was popular some 10 years ago, and may still be where you have large collections of final variables. Way 2, is simple to use but a little weird, since you can refer to constants without qualification, which makes your code difficult to comprehend.

方式 1,大约 10 年前流行,并且可能仍然是您拥有大量最终变量集合的地方。方式二,使用简单但有点奇怪,因为你可以不加限定地引用常量,这让你的代码难以理解。

回答by Peter Lawrey

One difference in having a final field, is that because String is immutable, it may as well be static.

有一个 final 字段的一个区别是,因为 String 是不可变的,它也可能是静态的。

private final String a = "test";

slightly more efficient, possibly clearer as

稍微更有效,可能更清楚

private static final String a = "test";