0% found this document useful (0 votes)
3 views

JAVA Basic Statements

The document provides an overview of basic Java programming statements including input/output statements, conditional statements (if and switch), loops (do-while and for), subroutines, arrays (including multidimensional arrays), built-in string functions, and file handling operations. It includes examples of declaring variables, creating and manipulating arrays, and performing file operations such as creating, reading, and deleting files. The content serves as a foundational guide for beginners in Java programming.

Uploaded by

Chitra Kanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA Basic Statements

The document provides an overview of basic Java programming statements including input/output statements, conditional statements (if and switch), loops (do-while and for), subroutines, arrays (including multidimensional arrays), built-in string functions, and file handling operations. It includes examples of declaring variables, creating and manipulating arrays, and performing file operations such as creating, reading, and deleting files. The content serves as a foundational guide for beginners in Java programming.

Uploaded by

Chitra Kanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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:

Built in Mathametical Functions:

Do .. While loop

do{

}while(condition);

For Loop

for(intialization;condtition; increment/decrement){}
Subroutine:

Defining a subroutine:

Public class fuction-name{

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

An array of objects is created like an array of primitive-type data items in the


following way.
Student[] arr = new Student[5]; //student is a user-defined
class

Multidimensional Arrays:

int[][] intArray = new int[10][20]; //a 2D array or matrix


int[][][] intArray = new int[10][20][10]; //a 3D array

public class multiDimensional {


public static void main(String args[])
{
// declaring and initializing 2D array
int arr[][]
= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };

// 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();
}
}
}

Built in string functions


Functions
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}

public static void main(String[] args) {


myMethod();

File Handling
Create a File and read a file
mport java.io.File; // Import the File class

import java.io.IOException; // Import the IOException class to


handle errors

public class CreateFile {


public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}

File creation in a specific folder


File myObj = new File("C:\\Users\\MyName\\filename.txt");

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.");
}
}
}

You might also like