是final String s =“Hello World”与String相同s =“Hello World”? [英] Is final String s = "Hello World" same as String s = "Hello World"?
问题描述
如果一个类被定义为 final
,我们声明一个最终类的实例...它会有什么区别吗?或者 final
在这种情况下是多余的?
final String s =Hello World
与
相同 String s =Hello World
解决方案对变量,这意味着它不能被重新分配。考虑下面的例子:
public class FinalExample {
private final String a =Hello World! //不能重新赋值
private String b =Goodbye World! //可以重新赋值
public FinalExample(){
a = b; // ILLEGAL:此字段不能重新分配
b = a;
}
public static void main(String [] args){
new FinalExample();
}
}
如果尝试运行它,错误在 a = b
:
线程mainjava中的异常.lang.Error:未解决的编译问题:
最终字段FinalExample.a不能在FinalExample中分配
< init>(FinalExample.java:7)
在FinalExample .main(FinalExample.java:12)
现在,我想你想知道a final
或不在前面的 String
数据类型。虽然你可能听说 String
是不可变的,你仍然可以重新分配 String s =Hello World!;
到另一个字符串值。这个区别是由于你实际上是把 String s
的引用重新赋值给一个新的字符串。因此,以下内容是有效的:
String s =Hello World
s =再见世界!
System.out.println(s);
产出:Goodbye World!
但是您可以使用 final
防止此更改:
final String s =Hello World;
s =再见世界!
System.out.println(s);
输出:
线程main中的异常java.lang.Error:未解决的编译问题:
无法分配最终局部变量s。它必须为空,不使用复合赋值
final
声明是很好的使用,如果你想确保没有人可以改变你的 String
引用。您也可以对其他数据类型使用 final
,以防止更改参考。
If a class is defined as final
and we declare an instance of the final class... Would it make any difference? or is final
in such cases would be redundant?
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. 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
:
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. Although you may have heard that String
is immutable, you can still re-assign something like 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. 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 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. You can use final
for other data types as well to prevent the reference from being changed.
这篇关于是final String s =“Hello World”与String相同s =“Hello World”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!