DEV Community

Cover image for Java: The Complete Beginner-to-Advanced Guide (With Practical Code Examples)
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

Java: The Complete Beginner-to-Advanced Guide (With Practical Code Examples)

Java remains one of the most influential programming languages in the world—powering enterprise applications, Android apps, financial systems, cloud platforms, and millions of backend services. If you want a language that is stable, scalable, secure, and widely used across industries, Java is an excellent choice.

In this article, you’ll learn Java from the ground up, including syntax, object-oriented programming, advanced concepts, and practical code examples. Whether you are new to programming or transitioning from another language, this guide will help you write clean, production-ready Java code.


1. What Is Java and Why Is It Popular?

Java is a class-based, object-oriented, platform-independent, strongly typed programming language.

Key Features

  • Platform Independent (“Write Once, Run Anywhere” – WORA)
  • Object-Oriented (OOP: classes, objects, inheritance, polymorphism)
  • Multi-threaded
  • Secure & Robust
  • Huge Ecosystem (Spring Boot, Maven, Gradle, JavaFX)
  • Massive Community Support

2. Setting Up Java

Install Java

Download JDK from:

https://jdk.java.net/
Enter fullscreen mode Exit fullscreen mode

Check version

java -version
javac -version
Enter fullscreen mode Exit fullscreen mode

3. Your First Java Program

Create a file named Main.java.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Compile:

javac Main.java
Enter fullscreen mode Exit fullscreen mode

Run:

java Main
Enter fullscreen mode Exit fullscreen mode

4. Variables and Data Types

Java is strongly typed, so every variable must have a type.

public class Main {
    public static void main(String[] args) {
        int age = 25;
        double price = 99.99;
        char grade = 'A';
        boolean isActive = true;
        String name = "Farhad";

        System.out.println(name + " - " + age);
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Control Flow Statements

If / Else

int score = 80;

if (score >= 90) {
    System.out.println("Excellent");
} else if (score >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}
Enter fullscreen mode Exit fullscreen mode

Switch

String day = "Monday";

switch (day) {
    case "Monday":
        System.out.println("Start of week");
        break;
    default:
        System.out.println("Unknown day");
}
Enter fullscreen mode Exit fullscreen mode

Loops

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

int j = 1;
while (j <= 5) {
    System.out.println(j);
    j++;
}
Enter fullscreen mode Exit fullscreen mode

6. Functions (Methods)

public class Main {
    static void greet(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {
        greet("Java Developer");
    }
}
Enter fullscreen mode Exit fullscreen mode

7. Arrays and Collections

Array

int[] numbers = {10, 20, 30};

for (int n : numbers) {
    System.out.println(n);
}
Enter fullscreen mode Exit fullscreen mode

ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Farhad");
        names.add("Rahimi");

        System.out.println(names);
    }
}
Enter fullscreen mode Exit fullscreen mode

8. Object-Oriented Programming (OOP)

Class and Object

class Car {
    String model;
    int year;

    void start() {
        System.out.println(model + " is starting...");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.model = "Toyota";
        c.year = 2024;
        c.start();
    }
}
Enter fullscreen mode Exit fullscreen mode

9. Constructors

class User {
    String name;
    int age;

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        User u = new User("Farhad", 23);
        System.out.println(u.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

10. Inheritance

class Animal {
    void sound() {
        System.out.println("Makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}
Enter fullscreen mode Exit fullscreen mode

11. Polymorphism

class Shape {
    void draw() {
        System.out.println("Drawing shape");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();  
        s.draw();
    }
}
Enter fullscreen mode Exit fullscreen mode

12. Encapsulation

class BankAccount {
    private double balance;

    public void deposit(double amount) {
        balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.deposit(100);
        System.out.println(acc.getBalance());
    }
}
Enter fullscreen mode Exit fullscreen mode

13. Abstraction (Interfaces & Abstract Classes)

Interface

interface Animal {
    void eat();
}

class Cat implements Animal {
    public void eat() {
        System.out.println("Cat eating");
    }
}
Enter fullscreen mode Exit fullscreen mode

Abstract Class

abstract class Vehicle {
    abstract void move();
}

class Bike extends Vehicle {
    void move() {
        System.out.println("Bike moves fast");
    }
}
Enter fullscreen mode Exit fullscreen mode

14. Exception Handling

try {
    int x = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
} finally {
    System.out.println("Always runs");
}
Enter fullscreen mode Exit fullscreen mode

15. File Handling

import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("output.txt");
            fw.write("Java file writing...");
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

16. Java and OOP in Real Projects

Java is widely used in:

  • Spring Boot (Enterprise Backends)
  • Android Development
  • Financial Services
  • Large-scale Microservices
  • Distributed Systems
  • Big Data with Hadoop
  • Cloud Applications (AWS, GCP, Azure)

Most production environments prefer Java due to its reliability and scalability.


17. A Mini Java Project: Simple To-Do Console App

import java.util.ArrayList;
import java.util.Scanner;

public class TodoApp {
    public static void main(String[] args) {
        ArrayList<String> tasks = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("1. Add Task  2. View Tasks  3. Exit");
            int choice = sc.nextInt();
            sc.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Enter task: ");
                    tasks.add(sc.nextLine());
                    break;
                case 2:
                    System.out.println("Your tasks:");
                    for (String t : tasks) System.out.println("- " + t);
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("Invalid choice");
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Java remains one of the most powerful languages for backend development, enterprise systems, Android applications, and scalable architectures. Its mature ecosystem, strong community, and long-term stability make it a top choice for developers worldwide.

Whether you're building microservices with Spring Boot, high-performance servers, or cross-platform applications, Java gives you the tools to succeed.

Top comments (0)