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

Java_Programs_Examples

The document provides a series of Java programming examples demonstrating various concepts such as printing output, addition of numbers, method overloading and overriding, interface implementation, exception handling, thread synchronization, inter-thread communication, and constructor overloading. Each example includes code snippets and their respective outputs. These examples serve as a practical guide for understanding fundamental Java programming techniques.

Uploaded by

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

Java_Programs_Examples

The document provides a series of Java programming examples demonstrating various concepts such as printing output, addition of numbers, method overloading and overriding, interface implementation, exception handling, thread synchronization, inter-thread communication, and constructor overloading. Each example includes code snippets and their respective outputs. These examples serve as a practical guide for understanding fundamental Java programming techniques.

Uploaded by

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

Java Programming Examples

1. Print Hello World

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
// Output: Hello, World!

2. Addition of Two Numbers

public class AddNumbers {


public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
// Output: Sum: 30

3. Method Overloading

class OverloadExample {
void show(int a) {
System.out.println("Int: " + a);
}
void show(String a) {
System.out.println("String: " + a);
}

public static void main(String[] args) {


OverloadExample obj = new OverloadExample();
obj.show(5);
obj.show("Hello");
}
}
// Output: Int: 5
// String: Hello

4. Method Overriding

class Parent {
void show() {
System.out.println("Parent's show()");
}
}
Java Programming Examples
class Child extends Parent {
void show() {
System.out.println("Child's show()");
}
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
// Output: Child's show()

5. Implement and Extends Together

interface Vehicle {
void drive();
}

class Engine {
void start() {
System.out.println("Engine Started");
}
}

class Car extends Engine implements Vehicle {


public void drive() {
System.out.println("Car is driving");
}

public static void main(String[] args) {


Car car = new Car();
car.start();
car.drive();
}
}
// Output: Engine Started
// Car is driving

6. This Keyword Usage

class ThisExample {
int a;

ThisExample(int a) {
this.a = a;
}

void display() {
System.out.println("Value: " + this.a);
}
Java Programming Examples

public static void main(String[] args) {


ThisExample obj = new ThisExample(10);
obj.display();
}
}
// Output: Value: 10

7. Exception Handling

public class ExceptionExample {


public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
// Output: Error: / by zero

8. Thread Synchronization

class Counter {
int count = 0;

synchronized void increment() {


count++;
}
}

public class SyncThread extends Thread {


Counter counter;

SyncThread(Counter c) {
this.counter = c;
}

public void run() {


for (int i = 0; i < 1000; i++) {
counter.increment();
}
}

public static void main(String[] args) throws Exception {


Counter c = new Counter();
SyncThread t1 = new SyncThread(c);
SyncThread t2 = new SyncThread(c);
t1.start();
Java Programming Examples
t2.start();
t1.join();
t2.join();
System.out.println("Count: " + c.count);
}
}
// Output: Count: 2000

9. Inter Thread Communication

class Shared {
boolean flag = false;

synchronized void produce() throws InterruptedException {


if (flag) wait();
System.out.println("Producing...");
flag = true;
notify();
}

synchronized void consume() throws InterruptedException {


if (!flag) wait();
System.out.println("Consuming...");
flag = false;
notify();
}
}

public class InterThread {


public static void main(String[] args) {
Shared s = new Shared();

new Thread(() -> {


try { s.produce(); } catch (Exception e) {}
}).start();

new Thread(() -> {


try { s.consume(); } catch (Exception e) {}
}).start();
}
}
// Output: Producing...
// Consuming...

10. Constructor Overloading

class Person {
String name;
int age;
Java Programming Examples

Person(String name) {
this.name = name;
this.age = 18; // default
}

Person(String name, int age) {


this.name = name;
this.age = age;
}

void show() {
System.out.println(name + " - " + age);
}

public static void main(String[] args) {


Person p1 = new Person("John");
Person p2 = new Person("Jane", 25);
p1.show();
p2.show();
}
}
// Output: John - 18
// Jane - 25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy