0% found this document useful (0 votes)
32 views61 pages

Multi Threading

Uploaded by

aditti4005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views61 pages

Multi Threading

Uploaded by

aditti4005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

PRESENTATION TITLE: WHAT IS

JAVA?

Subtitle: Understanding the Language,


Platforms & Applications

BY DEEPANSHU GUPTA AND ABHISHEK RANA


INTRODUCTIO

N
Java is a programming language used worldwide.
• It’s also a development environment and a
platform for building applications.
• Known for its portability, security, and robustness.
JAVA PLATFORMS OVERVIEW

• Java is divided into three main platforms:

• Core Java (J2SE)


• Advanced Java (J2EE)
• Java ME (J2ME)
CORE JAVA (J2SE)

• Used for standalone applications.


• Examples:
• - Notepad
• - Calculator
• - Medical software
• Runs on desktop systems.
ADVANCED JAVA
(J2EE)
• Focused on web applications.
• Combines:
⚬ Website structure
⚬ Dynamic content (e.g., user interaction,
databases)
• Powers enterprise-level systems.
JAVA ME
(J2ME)
Designed for mobile and embedded devices.
Used in Android app development.
Lightweight and optimized for limited resources.
BASIC JAVA PROGRAM – HELLO
WORLD
public class HelloWorld { • This is the simplest Java program.
public static void main(String[] args) { • It prints a message to the console.
System.out.println("Hello, World!"); • Every Java program starts with
} method.
}
SIMPLE LOGIC – AGE
VALIDATION
int age = 20;
if (age >= 18){
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
GRADING SYSTEM BASED ON
PERCENTAGE
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D");
}
DAY 2: JAVA BASICS – LOOPS & SWITCH
STATEMENTS

SYNTAX – WHILE LOOP

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:

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}
Java Basics – Pattern
Printing

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
}

public static void main(String[] args) {


Employee obj = new Employee();
obj.display();
}
}
METHOD WITH PARAMETERS
class EmployeeWithP {
void display(int a, int b) {
int s = a + b;
System.out.println("sum is " + s);
}

public static void main(String[] args) { sum is 23


EmployeeWithP obj = new
EmployeeWithP();
obj.display(12, 11);
}
}
CONSTRUCTOR WITH PARAMETERS

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

class child extends parent {


void display() {
int sum = a + b;
System.out.println(sum);
}

public static void main(String[] args) {


child obj = new child();
obj.display();
obj.input();
}
}
MULTILEVEL INHERITANCE
class grandfather {
int a = 10, b = 12;
}

class father extends grandfather {


int c = 15;
void input() {
int sum = a + b;
System.out.println(sum);
} 22
} 37
class son extends father {
void display() {
int total = a + b + c;
System.out.println(total);
}

public static void main(String[] args) {


son obj = new son();
obj.input();
obj.display();
}
}
HIERARCHICAL INHERITANCE
class parent1 {
int a = 10, b = 12, c = 0;
}

class child1 extends parent1 {


void display() {
int c = a + b;
System.out.println(c);
}
} -2
22
class child2 extends parent {
void display() {
int c = a - b;
System.out.println(c);
}

public static void main(String[] args) {


child1 obj = new child1();
child2 obj1 = new child2();
obj1.display();
obj.display();
}
}
INTERFACE IMPLEMENTATION
interface person {
void disp();
}

class department implements person {


public void disp() {
simple interface implementation
System.out.println("simple
interface implementation");
}

public static void main(String[] args) {


department obj = new
department();
obj.disp();
}
}
MULTIPLE INTERFACE
interface A {

}
void display();
IMPLEMENTATION
interface B {
void input();
}

class C implements A, B { hello


public void display() { world
System.out.println("hello");
}

public void input() {


System.out.println("world");
}

public static void main(String[] args) {


C obj = new C();
obj.display();
obj.input();
}
}
POLYMORPHISM IN JAVA
Definition: Polymorphism allows objects to take
on many forms by enabling methods to behave
differently based on context.

🔹 1. Static Polymorphism (Compile-Time)


Definition: Method binding occurs at compile
time using method overloading or overriding.
a. Method Overloading
Definition: Multiple methods share the same
name but differ in parameters.
1. NUMBER OF PARAMETER
disp(int,int)

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(int x, int y, int z) {


System.out.println(x + y + z);
}

public static void main(String[] args) {


person obj = new person();
obj.display(2, 22); // Output: 24
obj.display(22, 22, 203); // Output: 247
}
}
METHOD OVERLOADING – DIFFERENT PARAMETER
class person1 {
TYPES
void display(int x, int y) {
if (x > y) {
System.out.println(x);
} else {
System.out.println(y);
}
}

void display(float x, float y) {


if (x > y) {
System.out.println(x);
} else {
System.out.println(y);
}
}

public static void main(String[] args) {


person1 obj = new person1();
obj.display(2, 22); // Output: 22
obj.display(22.3f, 12.4f); // Output: 22.3
}
}
METHOD OVERLOADING – DIFFERENT
class person2 {
PARAMETER ORDER
void display(int age, String name) {
System.out.println(age);
System.out.println(name);
}

void display(String name, int age) {


System.out.println(name);
System.out.println(age);
}

public static void main(String[] args) {


person2 obj = new person2();
obj.display(22, "Davy"); // Output: 22 Davy
obj.display("Davy", 22); // Output: Davy 22
}
}
METHOD OVERRIDING WITH KEYWORD
class parent {
int x = 10, y = 2, z = 0;

void display() {
z = x + y;
System.out.println(z);
}
}

class child extends parent {


void display() {
z = x - y;
System.out.println(z); // Output: 8
super.display(); // Output: 12
}

public static void main(String[] args) {


child obj = new child();
obj.display();
}
}
class A { RUNTIME POLYMORPHISM – UPCASTING
void display() {
System.out.println("first class");
}
}
class B extends A {
void display() {
System.out.println("second class");
}
}
class C extends A {
void display() {
System.out.println("third class");
}
public static void main(String[] args) {
A obj = new A();
B obj1 = new B();
C obj2 = new C();
A z;
z = obj;
z.display(); // Output: first class
z = obj1;
z.display(); // Output: second class
z = obj2;
z.display(); // Output: third class
}
}
ARRAY TRAVERSAL

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

System.out.println(obj); // [1, 12, first, 12.3, second]


System.out.println(obj.size()); // 5

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.*;

HashSet class frame1 {


public static void main(String[] args) {

Example HashSet obj = new HashSet();


obj.add(1);
obj.add(12);
obj.add("first");
obj.add(12.3f);
obj.add("second");

System.out.println(obj); // Unordered set


System.out.println(obj.size()); // 5

obj.add(12); // Duplicate ignored


System.out.println(obj);
System.out.println(obj.size()); // Still 5

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 {

Example public static void main(String[] args) {


TreeSet obj = new TreeSet();
obj.add("5");
obj.add("4");
obj.add("3");
obj.add("2");
obj.add("1");

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); // Lexicographically ordered


System.out.println(obj.size()); // 5

System.out.println(obj.peek()); // aquaman
System.out.println(obj); // Still full queue
System.out.println(obj.size()); // 5

System.out.println(obj.poll()); // aquaman removed


System.out.println(obj); // Remaining elements
System.out.println(obj.size()); // 4
}
}
Autoboxing & class atun {
Unboxing public static void main(String[] args) {
int i = 10;
Example
// Autoboxing: converting int to Integer
Integer Ii = new Integer(i);
System.out.println(Ii); // Output: 10

// Unboxing: converting Integer to int


Integer m = new Integer(220);
int y = m;
System.out.println(y); // Output: 220
}
}
import java.util.*;
import static java.lang.System.*;

class queue {
public static void main(String[] args) {
ArrayDeque– ArrayDeque obj = new ArrayDeque();
obj.add("12");

Double-Ended Queue obj.add("15");


obj.add("17");
obj.add("10");
out.println(obj); // [12, 15, 17, 10]
out.println(obj.size()); // 4

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); // [12, 15, 17, 10]


out.println(obj.size()); // 4

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

out.println(obj); // {5=om, 4=bhagvate,


7=vasudev, 3=namah, 0=kk}
out.println(obj.containsKey("10")); // false
out.println(obj.keySet()); // [5, 4, 7, 3, 0]
}
}
import java.util.*;
import java.util.Date;
import java.util.Scanner;
import static java.lang.System.*;
Scanner & Date –
class sc {
User Input public static void main(String[] args) {
String name;
int age;
Scanner obj = new Scanner(in);
Date obj1 = new Date();

out.print("Enter the name = ");


name = obj.nextLine();

out.print("Enter the age = ");


age = obj.nextInt();

out.println("my name is = " + name);


out.println("my age is = " + age);
out.println(obj1); // Current system date and time
}
}
Enhanced For Loop – import static java.lang.System.*;

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) {}
}
}

public static void main(String[] args) {


mythread obj = new mythread();
Thread t1 = new Thread(obj);
t1.start();
}
}
Thread creation by
extending class class mythread1 extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
try {
System.out.println(i);
Thread.sleep(1);
} catch (Exception e) {}
}
}

public static void main(String[] args) {


mythread1 obj = new mythread1();
obj.start();
}
}
class mythread2 extends Thread {
Starting thread from void mythread2() {
start();
constructor }

public void run() {


for (int i = 1; i <= 10; i++) {
try {
System.out.println(i);
Thread.sleep(1);
} catch (Exception e) {}
}
}

public static void main(String[] args) {


mythread2 obj = new mythread2();
// Note: mythread2() is not a constructor,
so thread won't start unless explicitly called
}
}
class mythread1 extends Thread {
String name = Thread.currentThread().getName();
thread naming and public void run() {
System.out.println(name + " " +
priority Thread.currentThread().getPriority());
}
}

class mythread2 extends Thread {


String name = Thread.currentThread().getName();
public void run() {
System.out.println(name + " " +
Thread.currentThread().getPriority());
}

public static void main(String[] args) {


mythread1 obj1 = new mythread1();
mythread2 obj2 = new mythread2();
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.setName("venom");
t2.setName("goblin");
t1.setPriority(1);
t2.setPriority(10);
t1.start();
t2.start();
}
}

You might also like