Friday, April 18, 2008

Tips on Using loop statement in Java

Topic: Tips on using the loops statements
Source of Topic: Effective Java, Programming Language Guide, by Joshua Bloch.

"The most powerful technique for minimizing the scope of a local variable is to declare it
where it is first used."

Prefer
for loop over to while loop.
Reason:
for loop has a chance of initializing the loop variables, limiting the scope to exact region where they're needed.

For example, here is the preferred way of iterating over a collection:
for ( Iterator i = c.iterator(); i.hasNext(); i++ ) {
doSomething ( i.next() ) ;
}
To see why this for loop is preferable to the more obvious while loop, consider the following
code fragment

Iterator i = c.iterator();
while (i.hasNext()) {
doSomething(i.next());
}
...
Iterator i2 = c2.iterator();
while (i.hasNext()) { // BUG!
doSomethingElse(i2.next());
}


Above error is resulted with the habit of cut-and-paste the code: It initializes a new loop variable, i2, but uses the old one,
i, which unfortunately is still in scope. Above code doesn't give any compile error or doesn't throw any exception. Runs silently.
Instead of iterating over c2, the second loop terminates immediately, giving the false impression that c2 is empty. Because the
program errs silently, the error can remain undetected for a long time.

If the analogous cut-and-paste error were made in conjunction with the preferred for loop
idiom, the resulting code wouldn't even compile. The loop variable from the first loop would
not be in scope at the point where the second loop occurred:

for (Iterator i = c.iterator(); i.hasNext(); ) {
doSomething(i.next());
}
...
// Compile-time error - the symbol i cannot be resolved
for (Iterator i2 = c2.iterator(); i.hasNext(); ) {
doSomething(i2.next());
}


Moreover, if you use the for loop idiom, it's much less likely that you'll make the cut-and-
paste error, as there's no incentive to use a different variable name in the two loops. The loops
are completely independent, so there's no harm in reusing the loop variable name. In fact, it's
stylish to do so.

Iterating over random access list: ( example
s of random access lists: ArrayList, Vector etc. )
//Common Practice
for (int i = 0; i < list.size(); i++) {
doSomething(list.get(i));
}

// High-performance idiom for iterating over random access lists
for (int i = 0, list.size(); i < n; i++) {
doSomething(list.get(i));
}



This idiom is useful for random access List implementations such as ArrayList and Vector
because it is likely to run faster than the “preferred idiom” above for such lists. The important
thing to notice about this idiom is that it has two loop variables, i and n, both of which have
exactly the right scope. The use of the second variable is essential to the performance of the
idiom. Without it, the loop would have to call the size method once per iteration, which
would negate the performance advantage of the idiom. Using this idiom is acceptable when
you're sure the list really does provide random access; otherwise, it displays quadratic
performance.

Printing the arrays in Java programming language

// Common practice of printing the array in Java
for( int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
// Best way of printing the array in Java
System.out.println( Arrays.asList(array) ) ;

Loop over an Array in java:
// Loop through an Array using for each
for( int intVal : array ) {
doSomething(intVal);
}


2 comments:

Anonymous said...

Thanks for writing this.

Anonymous said...

This looks good.I appriciate your Interest over technical things.