java - Final keyword in for statement -


I was reading some code on the web and fell on these lines (Java):

 < Code> Private list & lt; String & gt; Values; For [...] (last string str: value) {length + = context.strlen (str); }  

What is the benefit of declaring a variable for loop? I thought the variable specified in the loop was already read (for example, in the example given above, for example 'str' can not assign something).

thx

What is the benefit of declaring one Variables in Variables for Variables?

There is not much in a small piece of code, but, if it will help to avoid changing the context during looping.

For example:

 for  (string s: value) {computeSomething (s); ... here's dozens of lines ... s = s.trim (); // or worst = getOtherString (); ... second-class compilation (s); }  

If you do not make the last use, then the last line other computation might possibly use a different value, which was defined in recurrence and While presenting a micro bug, while reading the second code, the controller will try to understand how the method is failing with the right values. <5 p>

For this, the 5 to 15 lines are easy to spot for long, but for the big segment code, I believe, it is very difficult.

for ... (last string: value) {s = new stringbilder (s) .reverse (). ToString (); }

This code fails in compile time variable s can already be assigned

Another benefit can be That variable can be used within a unique internal square:

 for  (last string s: value) {doSomething (new innerclass) {zero something () (S) ( S.length (); // internal square}});}  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -