Java 1.5+ features:
with 1.5
for(Employee emp : list )
{
//do something on employee object
}
What you have seen here:
Simple to write for loop, no type casting required, no need to check the size of list and increment it.
- For loop enhancements
- Generics
- Autoboxing/Unboxing
- Varargs
For loop enhancements:
As you know how we used to write for loop in 1.4 and if you see clearly some times we dont need to write more lines of code.
You can write simply like below with 1.5, assume you are iterating a ArrayList.
List list = new ArrayList(); //if you dont know about generics dont worry, will discuss //in we will discuss in next topic. assume this list has few employee
int size = list.size();
for(int i=0; i less than size; i plus plus)
{
Employee emp = (Employee)list.get(i); //Explicit type casting
//do something on employee object
}
with 1.5
for(Employee emp : list )
{
//do something on employee object
}
What you have seen here:
Simple to write for loop, no type casting required, no need to check the size of list and increment it.
No comments:
Post a Comment