☕ Java Programming Tutorial
Master object-oriented programming and enterprise development - from Java basics to advanced collections and frameworks.
01 What is Java?
Introduction to the Java ecosystem and the Write Once, Run Anywhere philosophy.
02 Setup & First Program
Install the JDK and write your very first Java class from scratch.
03 Variables & Types
Master primitive types and reference types in the Java language.
04 Operators
Perform arithmetic, logical, and comparison operations.
05 Control Flow
Control your program's execution with if-else and switch statements.
06 Loops
Repeat actions efficiently with for, while, and do-while loops.
07 Methods
Write modular code with parameters, return types, and overloading.
08 Classes & OOP
Introduction to the core of Java: Objects, Classes, and OOP principles.
📑 Table of Contents
Java Basics
Java is a versatile, object-oriented language known for its "write once, run anywhere" philosophy through the JVM. It's widely used in enterprise applications, Android development, and web services.
Installation & Setup:
- Download JDK (Java Development Kit) from oracle.com
- Install JDK and set JAVA_HOME environment variable
- Verify: Open terminal and type
java -version - Use IDE like IntelliJ IDEA, Eclipse, or VS Code
Your First Java Program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Variables & Data Types
// Primitive data types
int age = 25;
long population = 8000000000L;
float height = 5.9f;
double pi = 3.14159;
boolean isStudent = true;
char initial = 'A';
// Reference types
String name = "John Doe";
int[] numbers = {1, 2, 3, 4, 5};
// Type casting
int x = 10;
double y = (double) x; // Widening
double z = 10.5;
int w = (int) z; // Narrowing (10)
// Final (constant)
final int MAX_SIZE = 100;
// String operations
String greeting = "Hello" + " " + "World";
System.out.println("Name: " + name);
System.out.printf("Age: %d\n", age);
Control Flow
// If-Else
int score = 85;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("C");
}
// Switch
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other");
}
// For loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// Enhanced for loop
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.println(num);
}
// While loop
int count = 0;
while (count < 3) {
System.out.println(count);
count++;
}
Methods
public class Calculator {
// Method with parameters and return value
public static int add(int a, int b) {
return a + b;
}
// Method with no return
public static void greet(String name) {
System.out.println("Hello, " + name);
}
// Method with default parameter (Java doesn't support, use overloading)
public static void printMessage(String message) {
System.out.println(message);
}
public static void printMessage(String message, String name) {
System.out.println(message + ", " + name);
}
// Varargs (variable arguments)
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
public static void main(String[] args) {
System.out.println(add(5, 3)); // 8
greet("Alice"); // Hello, Alice
System.out.println(sum(1, 2, 3, 4)); // 10
}
}
Object-Oriented Programming
public class Car {
// Properties
private String brand;
private int year;
private double price;
// Constructor
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
}
// Getters
public String getBrand() {
return brand;
}
public int getYear() {
return year;
}
// Setters
public void setBrand(String brand) {
this.brand = brand;
}
// Method
public void display() {
System.out.println(brand + " (" + year + ")");
}
}
// Usage
Car car = new Car("Toyota", 2020);
car.display();
System.out.println(car.getBrand());
Inheritance
// Parent class
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class
public class Dog extends Animal {
public Dog(String name) {
super(name); // Call parent constructor
}
@Override
public void sound() {
System.out.println(name + " barks");
}
public void fetch() {
System.out.println(name + " fetches the ball");
}
}
// Usage
Dog dog = new Dog("Buddy");
dog.sound(); // Buddy barks
dog.fetch(); // Buddy fetches the ball
Interfaces & Abstract Classes
// Interface
public interface Shape {
void draw();
double getArea();
}
// Implementation
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing circle");
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// Abstract class
public abstract class Vehicle {
public abstract void start();
public void stop() {
System.out.println("Vehicle stopped");
}
}
Collections
import java.util.*;
// ArrayList
ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits.get(0)); // Apple
// LinkedList
LinkedList nums = new LinkedList<>();
nums.add(1);
nums.addFirst(0);
nums.addLast(2);
// HashMap
HashMap ages = new HashMap<>();
ages.put("John", 25);
ages.put("Jane", 28);
System.out.println(ages.get("John")); // 25
// HashSet
HashSet uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Bob");
uniqueNames.add("Alice"); // Won't be added again
// Iteration
for (String fruit : fruits) {
System.out.println(fruit);
}
Exception Handling
public class ExceptionExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Index out of bounds
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Generic error");
} finally {
System.out.println("Cleanup code");
}
}
// Custom exception
static class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
static void validateAge(int age) throws InvalidAgeException {
if (age < 0 || age > 150) {
throw new InvalidAgeException("Age must be between 0 and 150");
}
}
}
Projects & Practice
Beginner Projects:
- Student Management System
- Bank Account System
- Inventory Management
- Library Management System
- Simple Chat Application
Intermediate Projects:
- Spring Boot REST API
- E-commerce Application
- Ticket Management System
- File Management System
- Data Analysis Tool
Advanced Projects:
- Microservices Architecture
- Android Mobile App
- Distributed System
- High-traffic Web Application
- Game Development