[英]Is final String s = “Hello World” same as String s = “Hello World”?
If a class is defined as final
and we declare an instance of the final class... Would it make any difference? 如果一个类被定义为
final
,我们声明一个最终类的实例......它会有什么不同吗? or is final
in such cases would be redundant? 或者在这种情况下是
final
是多余的?
final String s = "Hello World"
same as 如同
String s = "Hello World"
When you use final
on the variable, it means that it cannot be re-assigned. 当您对变量使用
final
时,表示无法重新分配。 Consider the following example: 请考虑以下示例:
public class FinalExample {
private final String a = "Hello World!"; // cannot be reassigned
private String b = "Goodbye World!"; // can be reassigned
public FinalExample() {
a = b; // ILLEGAL: this field cannot be re-assigned
b = a;
}
public static void main(String[] args) {
new FinalExample();
}
}
If you try to run it, you will get an error on a=b
: 如果你试图运行它,你会在
a=b
上得到一个错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field FinalExample.a cannot be assigned
at FinalExample.<init>(FinalExample.java:7)
at FinalExample.main(FinalExample.java:12)
Now, I think you were wondering whether it matters to have a final
or not in front of the String
data type specifically. 现在,我想你想知道在
String
数据类型的前面是否具有final
是否重要。 Although you may have heard that String
is immutable, you can still re-assign something like String s = "Hello World!";
虽然您可能听说
String
是不可变的,但您仍然可以重新分配String s = "Hello World!";
to another string value. 到另一个字符串值。 This distinction is due to the fact that you are actually re-assigning the reference of
String s
to a new string. 这种区别是由于您实际上正在将
String s
引用重新分配给新字符串。 Therefore, the following is valid: 因此,以下内容有效:
String s = "Hello World";
s = "Goodbye World!";
System.out.println(s);
Output: Goodbye World!
But you can use the final
declaration to prevent this change: 但您可以使用
final
声明来阻止此更改:
final String s = "Hello World";
s = "Goodbye World!";
System.out.println(s);
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final local variable s cannot be assigned. It must be blank and not using a compound assignment
The final
declaration is great to use if you want to ensure that no one can change your String
reference. 如果您想确保没有人可以更改您的
String
引用,那么final
声明很有用。 You can use final
for other data types as well to prevent the reference from being changed. 您也可以将
final
用于其他数据类型,以防止引用被更改。
final
on a variable has nothing to do with whether the class is final
. 变量的
final
与该类是否为final
是无关的 。
final String s = "Hello World";
s = "Goodbye"; // illegal
String s2 = "Hello World";
s2 = "Goodbye"; // legal
A final variable cannot be bound to another instance, so this is not valid: 最终变量不能绑定到另一个实例,因此这是无效的:
final String s = "123";
s = "234"; // doesn't work!
A final class cannot be extended 最后一堂课不能延期
final class A {
}
class B extends A { // does not work!
}
And both of this is not to be confused with immutable classes (like String
). 并且这两者都不要与不可变类(如
String
)混淆。 Instances of immutable classes won't change their state. 不可变类的实例不会改变它们的状态。 (There is no direct language support for immutable classes, but in practice most of the time all attributes of immutable classes are declared final.)
(对于不可变类没有直接的语言支持,但实际上大多数情况下,不可变类的所有属性都被声明为final。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.