enhanced for loop in java 8 code example

Example 1: enhanced for loop java

class AssignmentOperator {
   public static void main(String[] args) {
      
      char[] vowels = {'a', 'e', 'i', 'o', 'u'};
      // foreach loop
      for (char item: vowels) {
         System.out.println(item);
      }
   }
}

Example 2: enhanced for-loop,

for (String element : array) {    System.out.println("Element: " + element);}

Example 3: enhanced for loop java

class ForLoop {
   public static void main(String[] args) {
      
      char[] vowels = {'a', 'e', 'i', 'o', 'u'};

      for (int i = 0; i < vowels.length; ++ i) {
         System.out.println(vowels[i]);
      }
   }
}

Example 4: enhanced for-loop,

for (int i=0; i < array.length; i++) {    System.out.println("Element: " + array[i]);}

Tags:

Java Example