W3Schools Java ForEach Tutorial Guide Examples

Robert Jul 13, 2026

Welcome to the world of Java, a modern object-oriented programming language that's widely used for developing enterprise-scale applications. One of its powerful features is the `foreach` loop, which provides a concise way to iterate through elements of an array or collection. Today, we're here to explore the `foreach` loop in Java, a topic covered comprehensively on the W3Schools Java tutorial.

W3Schools.com
W3Schools.com

The W3Schools Java "for each" loop tutorial (https://www.w3schools.com/java/java_foreach.asp) is a fantastic resource for beginners and experienced developers alike. It guides you through the fundamental aspects of using the `foreach` loop, from its syntax to its practical applications.

Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained

The Java `foreach` Loop: Syntax and Basics

The Java `foreach` loop, also known as the improved for loop, was introduced in Java 5.0. It offers a more readable and concise syntax compared to the traditional `for` loop. Its basic syntax is as follows:

Java Introduction
Java Introduction

```java for (type variable : array) { // code }```

Understanding the Syntax

Java Basics Cheat Sheet | Core Java Programming Notes for Beginners | AKTU Notes
Java Basics Cheat Sheet | Core Java Programming Notes for Beginners | AKTU Notes

The syntax consists of three components: the loop variable (type and name), the `:` operator, and the array or collection to iterate over. The loop variable takes on the value of each element in the collection in every iteration.

Let's quickly run through a simple example straight from the W3Schools tutorial. Suppose we have an array of integers:

```java int[] numbers = {1, 2, 3, 4, 5}; ```

β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€

Now, to print out each number in the `numbers` array, we could use the `foreach` loop like this:

```java for (int num : numbers) { System.out.println(num); } ```

Using `foreach` Loop with Collection Types

Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide

Remember, the `foreach` loop isn't limited to arrays. You can use it with any `Iterable` object such as `List`, `Set`, or `Map`. Here's an example using ArrayList from the W3Schools tutorial:

```java import java.util.ArrayList; ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); for (String car : cars) { System.out.println(car); } ```

Java Cheatsheet
Java Cheatsheet
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
10 Best Websites to Learn Java for Free in 2026
10 Best Websites to Learn Java for Free in 2026
Java Roadmap 2026 β˜• | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 β˜• | Complete Beginner to Advanced Learning Guide
β˜• What Is Java? | Simple Guide for Beginners πŸš€
β˜• What Is Java? | Simple Guide for Beginners πŸš€
Top 50 Java Projects
Top 50 Java Projects
How to Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
Learn Java in 30 Days Only
Learn Java in 30 Days Only
concept of java #java  #javanotes
concept of java #java #javanotes
Java Patterns Notes 1
Java Patterns Notes 1
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet πŸŽ―πŸ’»
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet πŸŽ―πŸ’»
Java Project Ideas For Beginners
Java Project Ideas For Beginners
60 Java projects ideasπŸ’‘
60 Java projects ideasπŸ’‘
java programs
java programs
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
File Handling in Java | Create Read Write Delete Files Easily
File Handling in Java | Create Read Write Delete Files Easily

The Benefits of Using `foreach` Loop in Java

Using the `foreach` loop in Java offers several advantages:

First, it simplifies the code by eliminating the need to write the initialization, condition, and increment parts of a traditional `for` loop. This results in cleaner, more readable code.

Secondly, it emphasizes the intent of the code. The `foreach` loop clearly shows that the goal is to iterate over a collection, making the code easier to understand and maintain.

Lastly, it's often safer, as you can't accidentally modify the size of the collection inside the loop.

Real-World Use Cases: Iterating Over Multidimensional Arrays

The `foreach` loop is particularly useful when working with multidimensional arrays. In the W3Schools tutorial, you'll find an example of iterating over a two-dimensional array (a matrix) using the `foreach` loop:

```java int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int[] row : numbers) { for (int num : row) { System.out.println(num); } } ```

As you can see, we use a nested `foreach` loop to iterate over each row, then each element in the row.

Discovering More with W3Schools

W3Schools provides numerous examples and exercises to help you understand and practice using the `foreach` loop. From iterating through string arrays to using it with enhanced `for` loop features like break and continue, each lesson builds upon the last, providing a solid foundation in this essential Java concept.

Remember, practice is key when learning to code. The more you practice, the more comfortable you'll become with the `foreach` loop and the better equipped you'll be to tackle more advanced topics in Java development.

So, what are you waiting for? Jump into the W3Schools Java `foreach` loop tutorial (https://www.w3schools.com/java/java_foreach.asp) and start honing your Java skills today. Happy coding!