A quickie just to get peace of mind:
Considering the following
final String str = "This is the end";
Is str.length()
evaluated at runtime or is it hardcoded as 15 in the bytecode?
str.length()
is calculated in String constructor
and saved in private final int count;
, str.length()
just returns the count
variable. I just checked the Source here http://www.docjar.com/html/api/java/lang/String.java.html
str.length()
is evaluated at runtime. final
means that value of the reference cannot be changed. It has nothing to do with the string itself.
However, if you look into the source code of String
you will see that length()
returns the value of a field, so no computation takes place, the value os simply read...
In the bytecode the method will be evaluated.
However, the method call will likely be inlined during jit compilation
The code for the String.length() method is as follows:
public int length() {
return count;
}
I don't think the fact that the reference to the string is declared final has any bearing on the inlining in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With