Java is an OOP language which has several in-built classes and methods for various purposes. One such example is the String class. The method length(), determines the length of the string.
For example,
String str = "hello";
for(int i=0;i<str.length();i++)
{
//code
}
Here, the condition is evaluated if the variable i is less then the length of the string, then the loop iterates. But, does it determine the length every time?
Is the method length(), called every time before the condition is evaluated? If that is the case, then does Java store the variable's value internally in a register for quick access (or) the programmer must do it explicitly like this:
String str = "hello";
int len = str.length();
for(int i=0;i<len;i++)
{
//code
}
How efficient is this approach?