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