Back to Modules

Java Tutorial

Master Java programming with our comprehensive tutorial. Learn object-oriented programming through examples and hands-on practice.

Full Playlist (Watch on Youtube)

Introduction to Java

Java is a class-based, object-oriented programming language designed to be platform-independent and secure.

Examples:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

A simple Hello World program in Java

Java Basics

Learn fundamental Java concepts including variables, data types, and basic operations.

Examples:

public class Main {
    public static void main(String[] args) {
        // Data types and variables
        int number = 42;
        double pi = 3.14159;
        char grade = 'A';
        boolean isValid = true;
        String name = "John";
        
        // Print all variables
        System.out.println("Number: " + number);
        System.out.println("Pi: " + pi);
        System.out.println("Grade: " + grade);
        System.out.println("Is Valid: " + isValid);
        System.out.println("Name: " + name);
    }
}

Common data types and variable declarations in Java

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Basic array declaration
        int[] numbers = {1, 2, 3, 4, 5};
        System.out.println("Array elements:");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
        System.out.println();
        
        // ArrayList example
        ArrayList<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Programming");
        
        System.out.println("ArrayList elements:");
        for (String item : list) {
            System.out.println(item);
        }
    }
}

Arrays and ArrayList examples with output

Object-Oriented Programming

Java is built around OOP principles: encapsulation, inheritance, polymorphism, and abstraction.

Examples:

public class Main {
    public static void main(String[] args) {
        Student student1 = new Student("Alice", 20);
        Student student2 = new Student("Bob", 22);
        
        student1.displayInfo();
        student1.study();
        
        student2.displayInfo();
        student2.study();
    }
}

class Student {
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void study() {
        System.out.println(name + " is studying.");
    }
    
    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

A simple class demonstrating OOP concepts with output

Control Flow and Loops

Learn how to control program flow using conditional statements and loops in Java.

Examples:

public class Main {
    public static void main(String[] args) {
        // If-else statement
        int score = 85;
        char grade;
        
        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else {
            grade = 'C';
        }
        System.out.println("Score: " + score + ", Grade: " + grade);
        
        // For loop
        System.out.println("For loop output:");
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
        
        // While loop
        System.out.println("While loop output:");
        int count = 0;
        while (count < 3) {
            System.out.println("Count: " + count);
            count++;
        }
    }
}

Examples of control flow statements and loops in Java