Skip to main content

Enhanced For Statement (for-each loop)

βœ” It is a new feature added in Java 5. βœ” It is also called as for-each loop. βœ” The enhanced for-loop is mainly used to access the elements from Collection/Array sequentially.

Syntax​

for(<dataType>varName :array|Collection) {
// Statements (BLOCK)
}

Limitations​

  • It can’t be used to access the data in reverse order
  • It can’t be used to access the index of elements
  • It can’t be used to access data alternatively (like 1st, 3rd, 5th elements, etc.)

Lab270.java​

class Lab270 {

public static void main(String[] args) {
int arr[] = { 99, 88, 66, 77 };

for (int i : arr) System.out.println(i);
}
}

Lab271.java​

class Lab271 {

public static void main(String[] args) {
String names[] = new String[] { "Java", "World", "Balan" };

for (String str : names) System.out.println(str);
}
}

Lab272.java​

class Lab272 {

public static void main(String[] args) {
int arr[] = null;

for (int i : arr) System.out.println(i);
}
}

Lab273.java​

class Lab273 {

public static void main(String[] args) {
char arr[] = { 'J', 'L', 'C', 'I', 'N', 'D', 'I', 'A' };

for (char ch : arr) System.out.println(ch);
}
}

3 Command Line Arguments**

βœ” Values provided by the user from command line while executing the Java program are called Command Line Arguments.

Syntax​

java <class Name> <val1> <val2> <val3> ...

Example​

java Hello1020 Javatrue9.9 A

πŸ’‘ Quick Understanding (useful for CodeNBuild)​

  • Enhanced for loop β†’ simple iteration βœ”
  • No index access
  • arr = null β†’ NullPointerException
  • Works best for read-only traversal

βœ” When you run the Hello program with the above line then following task will be done by JVM:​

  • βœ” Collects the values provided at command line after class name with space separation.
  • βœ” Constructs the String array with the size as per number of values provided.
  • βœ” Stores those values in String array.
  • βœ” Invokes the main method by passing String array as an argument.

Lab274.java​

class Lab274 {

public static void main(String[] args) {
System.out.println("Len : " + args.length);
System.out.println();

for (int i = 0; i < args.length; i++) {
System.out.println(i + "\t" + args[i]);
}
}
}

Run the above lab in various ways:​

1️⃣​

java Lab274
int size=0;
String args[]=new String[size];
Lab274.main(args);

2️⃣​

java Lab274123
int size=1;
String args[]=new String[size];
args[0]="123";
Lab274.main(args);

3️⃣​

java Lab274123 Atrue12.32 Java
int size=5;
String args[]=new String[size];
args[0]="123";
args[1]="A";
args[2]="true";
args[3]="12.32";
args[4]="Java";
Lab274.main(args);

4️⃣​

java Lab274 JavaWorld Midhun
int size=5;
String args[]=new String[size];
args[0]="Java";
args[1]="Learning";
args[2]="Center";
args[3]="Java";
args[4]="World";
Lab274.main(args);

5️⃣​

java Lab274"JavaWorld""Midhun"
int size=2;
String args[]=new String[size];
args[0]="JavaWorld";
args[1]="Midhun";
Lab274.main(args);

6️⃣​

java Lab274 *.java

πŸ“Œ Note:

  • OS will search the required files in the current working directory.
  • If found, then file names will be provided as Command Line Arguments.
  • Otherwise, the same value (.java) will be passed.

7️⃣​

java Lab274"*.java"

πŸ’‘ Quick Understanding​

  • Command line values β†’ always stored as String
  • Even numbers/boolean β†’ treated as String
  • Quotes " " β†’ group multiple words into one argument
  • JVM automatically creates and passes String[] args

Lab275.java​

class Lab275 {

public static void main(String[] args) {
if (args.length >= 2) {
String str1 = args[0];
String str2 = args[1];

System.out.println(str1 + str2);

int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);

System.out.println(a + b);
} else {
System.out.println("Enter two int values as CLA");
}
}
}

πŸ’‘ Quick Understanding​

  • args[0], args[1] β†’ values from command line
  • str1 + str2 β†’ String concatenation
  • Integer.parseInt() β†’ converts String β†’ int
  • Requires at least 2 inputs, otherwise shows message