devxlogo

The Enhanced for loop in Java 1.5 (5.0)

There have been a lot of enhancements to existing features in Java 1.5 (5.0). One of the simplest improvements is to the most commonly used for loop.

Here’s how it worked prior to version 1.5:

vec is a VectorSomeObject obj;for(int i=0;i<vec.size();i++){    obj = (SomeObject) aVec.elementAt(i);    //code to use obj}

Now, here's how it works in version 1.5:

for (Object aVec : vec){    obj = (SomeObject) aVec;    //code to use obj}
As you can see, the loop incrementor is now handled implicitly.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.