for loop and colon operator

My question is very simple and I appreciate any help. I found the following code in a text book and I don't know what is the propose of the "colon" operator inside the "for" loop.

// create four-element Employee array

Employee employees[] = new Employee[ 4 ];

// initialize array with Employees

employees[ 0 ] = salariedEmployee;

employees[ 1 ] = hourlyEmployee;

employees[ 2 ] = commissionEmployee;

employees[ 3 ] = basePlusCommissionEmployee;

System.out.println( "Employees processed polymorphically:\n" );

// generically process each element in array employees

for ( Employee currentEmployee : employees ){

System.out.println( currentEmployee ); // invokes toString

// determine whether element is a BasePlusCommissionEmployee

if ( currentEmployee instanceof BasePlusCommissionEmployee )

{

// downcast Employee reference to

// BasePlusCommissionEmployee reference

BasePlusCommissionEmployee employee =

( BasePlusCommissionEmployee ) currentEmployee;

double oldBaseSalary = employee.getBaseSalary();

employee.setBaseSalary( 1.10 * oldBaseSalary );

System.out.printf(

"new base salary with 10%% increase is: $%,.2f\n",

employee.getBaseSalary() );

} // end if

System.out.printf(

"earned $%,.2f\n\n", currentEmployee.earnings() );

} // end for

[1433 byte] By [estefanoa] at [2007-10-3 2:14:37]
# 1

That's the new "enhanced for loop" from Java 5.0. Basically you can read that line as "for every Employee in employees." It's basically the same as:

for(int i=0;i<employees.size();i++)

Employee currentEmployee = (Employee)employees.get(i);

Message was edited by:

TimFrey>

TimFreya at 2007-7-14 19:13:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Read the for-loop like this:for each currentEmployee in employees do the following {}So the colon basically means " in ".
Herko_ter_Horsta at 2007-7-14 19:13:27 > top of Java-index,Java Essentials,Java Programming...
# 3
Thank you for your explanation I cleared my doubt.
estefanoa at 2007-7-14 19:13:27 > top of Java-index,Java Essentials,Java Programming...
# 4
I believe the right answer isfor(int i=0;i<employees.length;i++)Employee currentEmployee = (Employee)employees;>
gluquea at 2007-7-14 19:13:27 > top of Java-index,Java Essentials,Java Programming...
# 5
In case you want more details... http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
jverda at 2007-7-14 19:13:27 > top of Java-index,Java Essentials,Java Programming...