JAVA Basic Statements
JAVA Basic Statements
Input statement:
Import java.util. Scanner;
Scanner myObj = new Scanner(System.in);
Output Statement:
System.out.println(“Enter a value”);
System.out.println(“answer is ” + a);
If statements:
If(condition)
{
}
else{
}
Case statement:
switch(var-name){
case 1: statement;
break;
case 2 : statement;
break;
}
Loops:
Do .. While loop
do{
}while(condition);
For Loop
for(intialization;condtition; increment/decrement){}
Subroutine:
Defining a subroutine:
return ;
Arrays:
type var-name[];
OR
type[] var-name;
Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
int[] intArray = new int[20]; // combining both statements in
one
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declaring array literal
Arrays of Objects
Multidimensional Arrays:
// printing 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
File Handling
Create a File and read a file
mport java.io.File; // Import the File class
Delete a file
import java.io.File;
public class DeleteFolder {
public static void main(String[] args) {
File myObj = new File("C:\\Users\\MyName\\Test");
if (myObj.delete()) {
System.out.println("Deleted the folder: " +
myObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}