I want to know if the length field of the array is kept internally as a value, or if it is calculated when called. Some sample code:
/* Makes several calls to string.length() in the body of the method */
String fooMyDataONE(String string, int queryLength) {
if(string.length() == queryLength) {
return workJobEquals(string);
}
else if(string.length() > queryLength) {
return workJobGreaterThan(string);
}
// else string.length < queryLength
return workJobLessThan(string);
}
/* Makes a single call to string.length() and stores it in a local variable */
String fooMyDataTWO(String string, int queryLength) {
int actualLength = string.length();
if(actualLength == queryLength) {
return workJobEquals(string);
}
else if(actualLength > queryLength) {
return workJobGreaterThan(string);
}
// else actualLength < queryLength
return workJobLessThan(string);
}
So what would be the pros/cons between the two methods? As a side thought I would also like to know how a compiler might treat the two methods in a Java class. I really just want to understand how it is implemented, I can not find those details in the javadoc.
edit: The other thread would answer my question if my question was aimed at the Java level. My question is aimed at the implementation, more at the byte code / machine code level. I have read several similar questions with the same information, and none of them give any implementation details, which is my question.