We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Pre declaring the variable allows the compiler to store the variable in a register, whereas str.length() forces a read of some memory location in RAM, so pre declaring is faster. I think if you declare the string as final and use str.length(), the compiler should do the same optimization. Even if you don't declare str as final, a smart compiler will see that you don't change 'str' and will optimize away the lookup. An even smarter compiler will see that the loop does nothing and will optimize the whole loop away. Best to look at the generated byte code.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Huffman Decoding
You are viewing a single comment's thread. Return to all comments →
Pre declaring the variable allows the compiler to store the variable in a register, whereas str.length() forces a read of some memory location in RAM, so pre declaring is faster. I think if you declare the string as final and use str.length(), the compiler should do the same optimization. Even if you don't declare str as final, a smart compiler will see that you don't change 'str' and will optimize away the lookup. An even smarter compiler will see that the loop does nothing and will optimize the whole loop away. Best to look at the generated byte code.