Java Programs from Basic to Advanced with Explanations
Beginner Programs
- Hello World
Explanation:
This is the simplest Java program. It defines a class called HelloWorld and the main method, which is the
entry point. It prints a message to the console.
Code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
- User Input
Explanation:
This program demonstrates how to take user input using the Scanner class. It reads an integer and prints it.
Code:
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
System.out.println("You entered: " + num);
Intermediate Programs
Java Programs from Basic to Advanced with Explanations
- Class and Object
Explanation:
Shows the basics of Object-Oriented Programming. A class defines properties and objects are created using
'new'.
Code:
class Car {
String color = "red";
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.color);
- Try-Catch
Explanation:
This program catches an arithmetic exception (division by zero). It's used to handle errors at runtime.
Code:
public class TryCatchExample {
public static void main(String[] args) {
try {
int divide = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
Advanced Programs
Java Programs from Basic to Advanced with Explanations
- Thread Example
Explanation:
Demonstrates multithreading. The Thread class is extended, and the thread is started using start().
Code:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
- Lambda Expression
Explanation:
This uses Java 8 lambda expressions to iterate and print list elements in a functional style.
Code:
import java.util.*;
public class LambdaDemo {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", "b", "c");
list.forEach(s -> System.out.println(s));
Mini Project
Java Programs from Basic to Advanced with Explanations
- Simple Inventory System
Explanation:
This is a basic inventory system using a HashMap. It allows adding items and viewing current stock. It's an
example of integrating multiple Java concepts: loops, conditionals, collections, and user input.
Code:
import java.util.HashMap;
import java.util.Scanner;
public class InventorySystem {
public static void main(String[] args) {
HashMap<String, Integer> inventory = new HashMap<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add item\n2. View inventory\n3. Exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (choice == 1) {
System.out.print("Enter item name: ");
String item = scanner.nextLine();
System.out.print("Enter quantity: ");
int qty = scanner.nextInt();
inventory.put(item, inventory.getOrDefault(item, 0) + qty);
} else if (choice == 2) {
System.out.println("Current Inventory:");
for (String key : inventory.keySet()) {
System.out.println(key + ": " + inventory.get(key));
} else {
break;
Java Programs from Basic to Advanced with Explanations