English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This is a pretty dumb question but still I want to confirm this... See the following code :

for(int i = 0; i < words.length; i++) {
// doing something here ..
}

Now for each iteration, is the words.length calculated ? I think the JVM is clever enough to replace words.length with the value and its not calculated everytime. Can someone confirm this ?

2007-02-15 21:47:14 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

I think words.length should be calculated every time. Although I didn't try it out using Java, I tried using JavaScript.



The script stopped at number 2. Try to translate this code into Java and try it out for yourself.

2007-02-15 22:16:05 · answer #1 · answered by papyrus 4 · 0 0

I believe that words.length is NOT calculated because it is a member variable and nto a function. if it was words.length() instead, then it would be calculated each time. In this case, the length is just looked up and checked each time during the loop. Theoretically, you could modify this value in the body of the loop.

2007-02-16 09:38:12 · answer #2 · answered by Lord_of_Armenia 4 · 0 0

fedest.com, questions and answers