Multi Threading
Multi Threading
JAVA?
declare variable;
while (test condition) {
statement;
increment/decrement;
}
PROGRAM – PRINT NUMBERS 1
TO 10
• OUTPU
T
• class Emp {
• public static void main(String[] args) { • 1
• int a = 1; • 2
• while (a <= 10) { • 3
• System.out.println(a); • 4
• • 5
a++;
• 6
• }}}
• 7
• 8
• 9
• 10
SWITCH STATEMENT
SYNTAX{
switch(variable)
case value1:
// statements
break;
case value2:
// statements
break;
default:
// fallback statements
}
PROGRAM – SWITCH
class Test { EXAMPLE
public static void main(String[] args) {
String ab = "java";
switch (ab) {
case "java":
System.out.println("Java is a programming language");
break;
case "python":
System.out.println("Python is a programming language");
break;
default: System.out.println("Invalid...");
}}}
NESTED FOR LOOP
PRINT A–Z USING ASCII
CONCEPT: ASCII VALUES OF TO ARE 97 TO 122.
class A {
public static void main(String[] args) {
char n = 97;
while (n < 123) {
System.out.println(n);
n++;
}}}
FOR LOOP SYNTAX:
class Emp { 1
public static void main(String[] args) 22
{ for (int i = 1; i <= 5; i++) {
333
for (int j = 1; j <= i; j++)
{ System.out.print(i); 4444
} 55555
System.out.print("\n");
}}}
INCREASING NUMBER PATTERN
class Emp2 {
public static void main(String[] args) { 1
int n = 1;
23
for (int i = 1; i <= 4; i++) {
456
for (int j = 1; j <= i; j++) {
78910
System.out.print(n++);
}
System.out.print("\n");
}
}
}
PYRAMID PATTERN WITH 1S
class Emp4 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("1"); 1
} 11
System.out.print("\n"); 111
} 1111
11111
for (int i = 1; i <= 5; i++) { 11111
for (int j = 5; j >= i; j--) { 1111
System.out.print("1");
111
}
11
System.out.print("\n");
1
}
}
}
JAVA BASICS – CLASSES, METHODS &
CONSTRUCTORS
CLASS WITH INSTANCE VARIABLES AND
class Employee { METHOD
String name = "Deepanshu";
int age = 22, day = 4;
void display() {
System.out.println("my name is " + name); my name is Deepanshu
System.out.println("my age is " + age); my age is 22
System.out.println("java training");
System.out.println("today is " + day + "th day of java training
training"); today is 4th day of training
}
class EmployeeWithC {
EmployeeWithC(int a, int b) {
int s = a + b;
System.out.println("sum is " + s);
}
sum is 335
public static void main(String[] args) {
EmployeeWithC obj = new EmployeeWithC(100,
235);
}
}
STATIC METHOD EXAMPLE
class EmployeeWithS {
static void display(int a, int b) {
int s = a + b;
System.out.println("sum is " + s);
}
sum is 23
public static void main(String[] args) {
display(12, 11);
}
}
OBJECT-ORIENTED PROGRAMMING (OOP)
CONCEPTS
Inheritance
Allows a class to acquire properties and behaviors from
another class.
Polymorphism
Enables one interface to be used for different
underlying data types or behaviors.
Encapsulation
Bundles data and methods together, restricting direct
access to internal details.
Abstraction
Hides complex implementation and shows only
essential features to the user.
SINGLE INHERITANCE
class parent {
int a = 10, b = 12;
void input() {
int sub = b - a;
System.out.println(sub);
} 22
} 2
}
void display();
IMPLEMENTATION
interface B {
void input();
}
disp(int,int,int)
2. type of
parameter
disp(int,int)
disp(float,float)
3. sequence of
B. METHOD OVERRIDING
parameter
Definition: Subclass provides a specific implementation
of a method already defined in its superclass.
disp(int,sting)
disp(string,int)
METHOD OVERLOADING – DIFFERENT NUMBER
OF PARAMETERS
class person {
void display(int x, int y) {
System.out.println(x + y);
}
void display() {
z = x + y;
System.out.println(z);
}
}
class array {
public static void main(String[] args) {
int m[] = {1, 12, 13, 15, 16};
for (int i = 0; i < m.length; i++) {
System.out.println(m[i]);
}
}
}
ArrayList
import java.util.*;
Example
class frame {
public static void main(String[] args) {
ArrayList obj = new ArrayList();
obj.add(1);
obj.add(12);
obj.add("first");
obj.add(12.3f);
obj.add("second");
obj.add(1, 22);
System.out.println(obj); // [1, 22, 12, first, 12.3,
second]
obj.add(0, "second");
System.out.println(obj); // [second, 1, 22, 12, first, 12.3,
second]
System.out.println(obj.size()); // 7
obj.clear();
System.out.println(obj.size()); // 0
import java.util.*;
obj.remove(12);
System.out.println(obj); // 12 removed
System.out.println(obj.size()); // 4
obj.add("adc");
System.out.println(obj); // adc added
System.out.println(obj.size()); // 5
obj.clear();
System.out.println(obj); // []
System.out.println(obj.size()); // 0
}
}
import java.util.*;
TreeSet class frame2 {
System.out.println(obj); // [1, 2, 3, 4, 5]
System.out.println(obj.size()); // 5
System.out.println(obj.pollFirst()); // 1
System.out.println(obj); // [2, 3, 4, 5]
System.out.println(obj.size()); // 4
System.out.println(obj.pollLast()); // 5
System.out.println(obj); // [2, 3, 4]
System.out.println(obj.size()); // 3
}
}
import java.util.*;
PriorityQueue
class frame3 {
Example public static void main(String[] args) {
PriorityQueue obj = new PriorityQueue();
obj.add("ben");
obj.add("villgax");
obj.add("venom");
obj.add("manta");
obj.add("aquaman");
System.out.println(obj.peek()); // aquaman
System.out.println(obj); // Still full queue
System.out.println(obj.size()); // 5
class queue {
public static void main(String[] args) {
ArrayDeque– ArrayDeque obj = new ArrayDeque();
obj.add("12");
obj.addFirst("om");
out.println(obj); // [om, 12, 15, 17, 10]
out.println(obj.size()); // 5
obj.addLast("namah");
out.println(obj); // [om, 12, 15, 17, 10, namah]
out.println(obj.size()); // 6
out.println(obj.getFirst()); // om
out.println(obj.getLast()); // namah
obj.pollFirst(); // removes om
obj.pollLast(); // removes namah
out.println(obj); // [12, 15, 17, 10]
out.println(obj.size()); // 4
}
}
import java.util.*;
import static java.lang.System.*;
class stk {
Stack– LIFO public static void main(String[] args) {
Stack obj = new Stack();
Structure obj.push("12");
obj.push("15");
obj.push("17");
obj.push("10");
out.println(obj.peek()); // 10
out.println(obj); // [12, 15, 17, 10]
out.println(obj.size()); // 4
out.println(obj.pop()); // 10
out.println(obj); // [12, 15, 17]
out.println(obj.size()); // 3
}
}
import java.util.*;
import static java.lang.System.*;
Iterator– Traversing
class itr {
a List public static void main(String[] args) {
ArrayList obj = new ArrayList();
obj.add("5");
obj.add("4");
obj.add("7");
obj.add("3");
Iterator it = obj.iterator();
while (it.hasNext()) {
out.println(it.next()); // 5 4 7 3
}
}
}
import java.util.*;
import static java.lang.System.*;
HashMap – Key-Value
class hm {
Pairs public static void main(String[] args) {
HashMap obj = new HashMap();
obj.put("5", "om");
obj.put("4", "bhagvate");
obj.put("7", "vasudev");
obj.put("3", "namah");
obj.put("0", "kk");
Array Traversal
class a {
public static void main(String[] args) {
int arr[] = {1, 12, 0, 15, 14};
for (int i : arr) {
out.println(i); // 1 12 0 15 14
}
}
}
Static Import – import static java.lang.System.*;
System.out Shortcut
class ab {
public static void main(String[] args) {
out.println("hello"); // hello
out.println("hi"); // hi
}
}
class excp {
public static void main(String[] args) {
Exception Handling int sum = 0;
try {
Multiple Exception type for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
} catch (NumberFormatException |
ArithmeticException e) {
System.out.println("pass integer value
only" + e);
}
System.out.println(sum);
}
}
class excp0 {
public static void main(String[] args) {
int sum = 0;
Separate catch try {
blocks for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
} catch (NumberFormatException e) {
System.out.println("pass integer value
only" + e);
} catch (ArithmeticException e1) {
System.out.println(e1);
}
System.out.println(sum);
}
}
class excp1 {
public static void main(String[] args) {
int sum = 0;
Generic Exception try {
catch for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
} catch (Exception e) {
System.out.println("pass integer value
only" + e);
}
System.out.println(sum);
}
}
class excp2 {
public static void main(String[] args) {
int sum = 0;
Try catch finally try {
block for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
} catch (Exception e) {
System.out.println("pass integer value
only" + e);
} finally {
System.out.println("always execute");
}
System.out.println(sum);
}
}
class excp3 {
public static void main(String[] args) {
int sum = 0;
try finally without try {
catch for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
} finally {
System.out.println("always execute");
System.out.println(sum);
}
}
}
file handling and
console I/O import java.io.*;
class person {
public static void main(String[] args) throws
IOException {
Writing to a file using filewriter FileWriter obj = new FileWriter("ab.txt");
obj.write("kcc institute");
obj.append("I am good student");
obj.close();
}
}
import java.io.*;
Reading console
class person1 {
input using public static void main(String[] args) throws
IOException {
bufferedreader String name;
int age;
try {
BufferedReader br = new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter name ");
name = br.readLine();
System.out.println("enter age ");
age = Integer.parseInt(br.readLine());
System.out.println(" name " + name);
System.out.println(" age " + age);
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.io.*;
Reading console
class person2 {
input using public static void main(String[] args) throws
IOException {
datainputstream String name;
int age;
try {
DataInputStream br = new
DataInputStream(System.in);
System.out.println("enter name ");
name = br.readLine();
System.out.println("enter age ");
age = Integer.parseInt(br.readLine());
System.out.println(" name " + name);
System.out.println(" age " + age);
} catch (Exception e) {
System.out.println(e);
}
}
}
Readind from a file
using filereader and import java.io.*;
bufferedreader class person3 {
public static void main(String[] args) throws
IOException {
PrintWriter venom = new
PrintWriter(System.out, true);
FileReader obj = new FileReader("ab.txt");
BufferedReader br = new
BufferedReader(obj);
venom.println(br.readLine());
}
}
Multithreading class mythread implements Runnable {
public void run() {
thread creation using Runnable for (int i = 1; i <= 10; i++) {
interface try {
System.out.println(i);
Thread.sleep(1);
} catch (Exception e) {}
}
}