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 linestr1 + str2β String concatenationInteger.parseInt()β converts String β int- Requires at least 2 inputs, otherwise shows message