Mon*_*ded 10 java string final
快速安心只是为了让您高枕无忧:
考虑以下因素
final String str = "This is the end";
Run Code Online (Sandbox Code Playgroud)
是str.length()在运行时评估还是在字节码中硬编码为15?
Nan*_*ale 13
str.length()计算String constructor并保存private final int count;,str.length()只返回count变量.我刚刚查看了这里的来源http://www.docjar.com/html/api/java/lang/String.java.html
Mat*_*arz 12
str.length() 是在运行时评估.final表示不能更改引用的值.它与字符串本身无关.
但是,如果您查看源代码,String您将看到length()返回字段的值,因此不会进行任何计算,值os只是读取...