Java

Core Java Tutorial for Beginners with Examples

A core Java tutorial for beginners in 10 steps: set up the JDK, then learn variables, loops, methods, arrays, classes, inheritance, collections and exceptions.

1Hello Java2Variables3Loops4Methods5ClassesCollections
  • Java
  • Tutorial
  • Beginners

Quick answer: To learn Core Java, install JDK 17 or newer, then work through ten steps in order: your first program, variables and types, operators, conditions, loops, methods, arrays and strings, classes and objects, inheritance and interfaces, and finally collections and exceptions. Type and run every example yourself, and change it before moving on.

This core Java tutorial takes you from an empty folder to programs that use classes, lists and exceptions. There are ten steps. Each has one complete program, its real output and a small task. Every program was compiled and run on Java 17.

You do not need any coding background. You need a laptop, about an hour a day and the patience to type code instead of copying it.

Before you start: how to learn core Java

Core Java means the language itself and its standard library: syntax, OOP, strings, collections, exceptions and threads. Frameworks such as Spring Boot come later and are built on top of it.

Three rules make the steps below work:

  1. Type every example. Your fingers learn the syntax faster than your eyes.
  2. Break it on purpose. Remove a semicolon, change a type, and read the error.
  3. Do the task at the end of each step before you move on.

Note: Core Java for beginners feels slow for the first week because of words like public static void. Accept them for now. Step 6 and Step 8 explain them.

Steps 1 to 3: Setup, variables and operators

Step 1: Install Java and run your first program

Install JDK 17 or newer. Our install Java guide shows the steps for Windows. Check it in a terminal with java -version.

Create a file called Hello.java. The file name must match the class name.

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

Run it with two commands: javac Hello.java compiles your code into bytecode, and java Hello runs it on the JVM. Every Java program starts from main.

Task: Print your name and your college on two lines.

Step 2: Store values in variables

A variable is a named box with a fixed type.

public class Profile {
    public static void main(String[] args) {
        String name = "Anjali";
        int age = 21;
        double cgpa = 8.4;
        boolean placed = false;
        char grade = 'A';
        System.out.println(name + " is " + age + " years old.");
        System.out.println("CGPA: " + cgpa + ", grade " + grade + ", placed: " + placed);
    }
}
Anjali is 21 years old.
CGPA: 8.4, grade A, placed: false

Java has eight primitive types: byte, short, int, long, float, double, char and boolean. String is a class, which is why it starts with a capital letter. Read more in our page on Java data types.

Task: Add a long variable for a phone number and print it.

Step 3: Calculate and compare

public class Operators {
    public static void main(String[] args) {
        int a = 17, b = 5;
        System.out.println(a + b);
        System.out.println(a / b);
        System.out.println(a % b);
        System.out.println((double) a / b);
        System.out.println(a > b && b > 10);
    }
}
22
3
2
3.4
false

17 / 5 is 3 because both numbers are int. The % operator gives the remainder. Cast one side to double when you need decimals.

Task: Convert 100 degrees Celsius to Fahrenheit with f = c * 9 / 5 + 32.

Steps 4 and 5: Conditions and loops

Step 4: Make decisions with if and switch

public class Grade {
    public static void main(String[] args) {
        int marks = 72;
        if (marks >= 75) {
            System.out.println("Distinction");
        } else if (marks >= 60) {
            System.out.println("First class");
        } else {
            System.out.println("Keep going");
        }

        String day = "SAT";
        String type = switch (day) {
            case "SAT", "SUN" -> "Holiday";
            default -> "College day";
        };
        System.out.println(type);
    }
}
First class
Holiday

The arrow form of switch works from Java 14 onwards and does not fall through to the next case. Older code uses case "SAT": with break.

Task: Print whether a number is positive, negative or zero.

Step 5: Repeat work with loops

public class Loops {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.print(i * 7 + " ");
        }
        System.out.println();

        int n = 1234, sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        System.out.println("Digit sum: " + sum);
    }
}
7 14 21 28 35 
Digit sum: 10

Use for when you know how many times to repeat. Use while when you repeat until something changes. break leaves a loop early, and continue skips to the next round.

Task: Print all even numbers from 1 to 20.

Steps 6 and 7: Methods, arrays and strings

Step 6: Write methods

A method is a named block of code that you can call many times.

public class Methods {
    static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        for (int n = 1; n <= 20; n++) {
            if (isPrime(n)) System.out.print(n + " ");
        }
        System.out.println();
    }
}
2 3 5 7 11 13 17 19 

static means you can call the method without creating an object. boolean is the return type. int n is the parameter.

Task: Write factorial(int n) that returns a long, and print factorial(15).

Step 7: Work with arrays and strings

import java.util.Arrays;

public class ArraysStrings {
    public static void main(String[] args) {
        int[] marks = {67, 82, 45, 91, 58};
        int max = marks[0];
        for (int m : marks) {
            if (m > max) max = m;
        }
        Arrays.sort(marks);
        System.out.println("Max: " + max);
        System.out.println(Arrays.toString(marks));

        String word = "Madam";
        String rev = new StringBuilder(word).reverse().toString();
        System.out.println(word.equalsIgnoreCase(rev) ? "Palindrome" : "Not a palindrome");
        System.out.println(word.toUpperCase() + " has " + word.length() + " letters");
    }
}
Max: 91
[45, 58, 67, 82, 91]
Palindrome
MADAM has 5 letters

Arrays have a fixed size and start at index 0. Strings cannot be changed, so methods like toUpperCase() return a new string. Compare text with equals(), never ==. Our page on Java strings has more core Java examples.

Task: Count the vowels in a sentence.

Steps 8 and 9: Classes, objects and inheritance

Step 8: Create your own class

A class is a blueprint. An object is one thing built from it.

public class BankAccount {
    private final String holder;
    private double balance;

    public BankAccount(String holder, double opening) {
        this.holder = holder;
        this.balance = opening;
    }

    public void withdraw(double amount) {
        if (amount > balance) {
            System.out.println("Insufficient balance for " + holder);
            return;
        }
        balance -= amount;
    }

    public double getBalance() { return balance; }

    public static void main(String[] args) {
        BankAccount acc = new BankAccount("Kiran", 5000);
        acc.withdraw(1200);
        acc.withdraw(10000);
        System.out.println("Balance: " + acc.getBalance());
    }
}
Insufficient balance for Kiran
Balance: 3800.0

The fields are private, so other code cannot set balance to any number it likes. It must go through withdraw, which checks the rule. This is encapsulation. The constructor runs once, when new creates the object.

Task: Add a deposit method that refuses negative amounts.

Step 9: Reuse code with inheritance and interfaces

interface Payable {
    double monthlyPay();
}

abstract class Employee implements Payable {
    protected final String name;
    Employee(String name) { this.name = name; }
}

class FullTime extends Employee {
    private final double salary;
    FullTime(String name, double salary) { super(name); this.salary = salary; }
    public double monthlyPay() { return salary / 12; }
}

class Intern extends Employee {
    private final double stipend;
    Intern(String name, double stipend) { super(name); this.stipend = stipend; }
    public double monthlyPay() { return stipend; }
}

public class Payroll {
    public static void main(String[] args) {
        Employee[] team = { new FullTime("Ravi", 480000), new Intern("Sneha", 15000) };
        for (Employee e : team) {
            System.out.println(e.name + ": " + e.monthlyPay());
        }
    }
}
Ravi: 40000.0
Sneha: 15000.0

extends makes a child class. super(name) calls the parent constructor. The loop calls monthlyPay() on each employee, and Java picks the right version at run time. That is polymorphism. An interface says what a class must do, and an abstract class can share fields and code.

Task: Add a Contractor class paid by the hour.

Step 10: Collections and exceptions

Real programs rarely know their size in advance, so they use collections instead of arrays. They also have to cope with bad input.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Inventory {
    public static void main(String[] args) {
        List<String> orders = new ArrayList<>(List.of("pen", "book", "pen", "bag", "pen"));
        Map<String, Integer> count = new HashMap<>();
        for (String item : orders) {
            count.put(item, count.getOrDefault(item, 0) + 1);
        }
        System.out.println("pen ordered " + count.get("pen") + " times");

        String[] inputs = {"12", "abc"};
        for (String s : inputs) {
            try {
                int qty = Integer.parseInt(s);
                System.out.println("Quantity: " + qty);
            } catch (NumberFormatException e) {
                System.out.println("Not a number: " + s);
            } finally {
                System.out.println("Checked " + s);
            }
        }
    }
}
pen ordered 3 times
Quantity: 12
Checked 12
Not a number: abc
Checked abc

ArrayList grows as you add items. HashMap stores key and value pairs and finds a value by its key quickly. The try block holds code that might fail, catch handles one type of failure, and finally always runs.

Task: Store five students in a HashMap of roll number to name, then print the name for one roll number.

Common mistake: Moving to Spring Boot before Step 10 feels easy. Spring code is full of lists, maps, interfaces and exceptions. If those are shaky, every framework error will feel confusing.

After this core Java tutorial

You now know enough to learn core Java properly by practice. Here is what to do next:

  1. Practise. Solve the 15 core Java practice problems without looking at the answers.
  2. Avoid the usual traps. Read our list of common core Java mistakes.
  3. Learn the rest of Core Java: static and final, generics, the Stream API, file handling and multithreading.
  4. Plan your time. Our guide on how long it takes to learn core Java gives a week-by-week plan.
  5. Build one console project, such as a library or hostel management app.

Core Java is the base of the Java full stack path. After it come SQL, JDBC, Hibernate, Spring Boot and React. Follow the Java full stack developer roadmap for the full order, or read the Java full stack course guide to see how a structured course covers each part.

Frequently asked questions

Can I learn Core Java without any coding background?

Yes. Core Java is a common first language for engineering students from every branch. Start with small programs, type every example yourself, and practise for about an hour a day.

Which Java version should a beginner install?

Install a long-term support version, such as JDK 17 or JDK 21. Every example in this tutorial runs on Java 17 and newer.

How long does it take to learn Core Java?

At one to two hours of practice a day, most beginners need about 3 to 4 months to become comfortable with Core Java, including OOP, collections and exceptions. At 3 to 4 hours a day, 6 to 8 weeks is realistic.

Should I use an IDE or Notepad to learn Java?

Write your first few programs in a plain editor and run them with javac and java, so that you understand compiling. Then move to IntelliJ IDEA Community Edition or VS Code for daily practice.

What should I learn after Core Java?

Learn SQL next, then JDBC, Hibernate and Spring Boot for the backend. For full stack roles, add HTML, CSS, JavaScript and React.


Want to learn Core Java with a trainer who reviews your code? Book a free demo class in our Madhapur, Hyderabad classroom or live online, or see the Java Full Stack with AI course.

Found this useful? Share it: LinkedIn Facebook WhatsApp

Written by

Siva Prasad Galaba

Staff Engineer & Founder, CodeBegun

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaching the next generation to code the way the industry actually works.

Back to all articles
Chat with us