1.
Java Program using command line arguments
// CommandLineExample.java
public class CommandLineExample {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + i + ": " + args[i]);
Output:
Number of arguments: 2
Argument 0: Hello
Argument 1: World
2.Understand OOP Concepts – Class , Object ,
Method
// OOPExample.java
class Car {
String color = "Red";
void display() {
System.out.println("Color of the car is: " + color);
public static void main(String[] args) {
Car myCar = new Car(); // object creation
myCar.display(); // calling method
Output:
Color of the car is: Red
3.Inheritance and Polymorphism
class Animal {
void sound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
public static void main(String[] args) {
Animal a = new Dog(); // Polymorphism
Output:
Dog barks
4.Exception Handling and Multithreading
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
Output:
Thread is running
5.Java Packages Example
package mypack;
public class PackageExample {
public void show() {
System.out.println("This is from my package");
public static void main(String[] args) {
PackageExample obj = new PackageExample();
obj.show();
Output:
This is from my package
6.Java I/O Example
import java.io.*;
public class IOExample {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter name: ");
String name = reader.readLine();
System.out.println("Hello " + name);
Output:
Enter name: Krati
Hello Krati
7.Interface Implementation
interface Animal {
void sound();
class Cat implements Animal {
public void sound() {
System.out.println("Meow");
public static void main(String[] args) {
Cat c = new Cat();
c.sound();
Output:
Meow
8.File Handling Example
import java.io.FileWriter;
import java.io.IOException;
public class FileWrite {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Writing to file in Java");
writer.close();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("Error occurred.");
Output:
File written successfully
9.Exception Handling
public class ExceptionExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
Output:
Cannot divide by zero
10.Array Example
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for(int i : arr) {
System.out.println(i);
Output:
3
11.String Operations
public class StringExample {
public static void main(String[] args) {
String s = "Hello";
System.out.println("Length: " + s.length());
System.out.println("Uppercase: " + s.toUpperCase
());
Output:
Length: 5
Uppercase: HELLO
12.Constructor Overloading
class Car {
Car() {
System.out.println("Default Constructor");
Car(String name) {
System.out.println("Car name: " + name);
public static void main(String[] args) {
new Car();
new Car("BMW");
Output:
Default Constructor
Car name: BMW
13.Static Keyword Usage
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println(count);
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
Output:
3
14. Super Keyword Demo
class Base {
void show() {
System.out.println("Base class");
classDerivedextendsBase{ void show() {
super.show(); System.out.println("Derivedclass");
public class SuperKeyword {
publicstaticvoidmain(String[]args){ Derived d = new Derived();
d.show();
Output:
Base class
Derived class
15.Nested Try-Catch Block
public class NestedTryCatch {
publicstaticvoidmain(String[]args){ try {
try {
int a = 30 / 0;
} catch (ArithmeticException e)
{ System.out.println("ArithmeticException");
int[]arr=newint[5]; arr[7] = 3;
} catch (ArrayIndexOutOfBoundsException e)
{ System.out.println("Arrayindexoutofbound");
Output:
Arithmetic Exception
Arrayindexoutofbound