Java 方法(字符串str)和方法(最终字符串str)之间的区别是什么
在方法参数中使用Final关键字让我感到困惑Java 方法(字符串str)和方法(最终字符串str)之间的区别是什么,java,methods,arguments,keyword,Java,Methods,Arguments,Keyword,在方法参数中使用Final关键字让我感到困惑 public void helloWorld(String str) { } public void helloWorld(Final String str) { } What is the differences between them? public void helloWorldfinal String str不允许在方法内部本地更改str的值,而如果没有final关键字,则可以在本地更改它 public void helloWo
public void helloWorld(String str)
{
}
public void helloWorld(Final String str)
{
}
What is the differences between them?
public void helloWorldfinal String str不允许在方法内部本地更改str的值,而如果没有final关键字,则可以在本地更改它
public void helloWorld(String str)
{
str = "something"; // OK
}
public void helloWorld(final String str)
{
str = "something"; // ERROR
}
当然,即使是helloWorldString str也不能更改传递给该方法的字符串的值,因为Java是一种按值传递语言。还有int或任何其他数据类型。@相信int和任何其他数据类型的答案都是一样的。