Java Tutorial — Learn Java Step by Step

Key takeaway: This free Java tutorial teaches you object-oriented programming, collections, exception handling, streams, and multithreading — with runnable code examples for each topic.

Java is a general-purpose, object-oriented programming language used for building Android apps, enterprise software, web backends, and cloud services. This tutorial is for beginners, CS students, and developers transitioning to Java.

Last updated: September 2026

Reference guide: Programiz — Getting Started with Java [0]

Java Syntax Basics

Understanding Java syntax is the foundation for writing any Java program. Java is a statically-typed, object-oriented language with C-style syntax and strict rules about structure, naming, and formatting.

Program Structure

Every Java program consists of at least one class with a main method as the entry point:

HelloWorld.java
// File name must match the public class name
public class HelloWorld {
    // main method — program entry point
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • public class HelloWorld — class declaration; filename must be HelloWorld.java
  • public static void main(String[] args) — the exact method signature the JVM looks for to start execution
  • System.out.println() — prints text followed by a newline to the console

Statements and Blocks

statements.java
// Statements end with a semicolon ;
int x = 10;
String name = "Alice";
System.out.println(name);

// Blocks are enclosed in curly braces { }
if (x > 5) {
    System.out.println("x is greater than 5");
    x = x + 1;
}

// Multiple statements per line (legal but not recommended)
int a = 1; int b = 2; int c = a + b;

Comments

comments.java
// Single-line comment — explains one line or a short note

/* Multi-line comment
   Use for longer explanations
   that span multiple lines */

/**
 * Javadoc comment — generates API documentation
 * @param name the user's name
 * @return a greeting string
 */
public String greet(String name) {
    return "Hello, " + name;
}

Use single-line comments for brief notes, multi-line for disabling blocks of code, and Javadoc comments on public methods and classes so tools like javadoc can generate HTML documentation automatically.

Naming Conventions

ElementConventionExample
Classes & InterfacesPascalCaseBankAccount, Runnable
Methods & VariablescamelCasecalculateTotal(), firstName
ConstantsUPPER_SNAKE_CASEMAX_SIZE, PI
Packagesall lowercasecom.example.app

Import Statements

imports.java
// Import specific class
import java.util.ArrayList;
import java.util.HashMap;

// Import entire package (use sparingly)
import java.util.*;

// Static import for utility methods
import static java.lang.Math.sqrt;
import static java.lang.Math.PI;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        double result = sqrt(PI);
    }
}

Access Modifiers

access.java
public class User {
    public String name;       // accessible from anywhere
    private int age;          // only within this class
    protected String email;   // this class + subclasses + same package
    String role;              // package-private (default, no keyword)

    public User(String name, int age) {
        this.name = name;     // 'this' refers to current object
        this.age = age;
    }

    public int getAge() {     // getter provides controlled access
        return this.age;
    }
}

Key Takeaways

  • Case-sensitive:Main and main are completely different identifiers.
  • Every statement ends with ; — forgetting it is the most common beginner error.
  • File name = class name: A file containing public class Foo must be named Foo.java.
  • Braces define scope: Variables declared inside { } are only visible within that block.
  • Follow naming conventions: PascalCase for classes, camelCase for methods/variables, UPPER_CASE for constants.
  • Always use braces even for single-line if/for/while blocks to prevent bugs when adding lines later.
Best Practice: Configure your IDE (IntelliJ, Eclipse) to auto-format code on save. Consistent formatting eliminates style debates and makes code reviews faster.

Java Tutorial FAQs

How do I run Java online?
Use our free online compiler. Click "Try Java Online" above to write and execute Java directly in your browser — no setup needed.
How do I install Java on Windows?
Download the JDK installer, run it, then set JAVA_HOME and update the Path variable to include the JDK bin directory. Verify with java --version. [0]
How do I install Java on macOS?
Install the appropriate JDK DMG (x64 or ARM64). In your shell profile, set export JAVA_HOME=$(/usr/libexec/java_home) and update PATH to include $JAVA_HOME/bin. Verify with java --version. [0]
Do I need the JDK to compile Java?
Yes. The JDK provides the compiler (javac) and tools needed to build Java applications. For quick tests, our online Java compiler is a convenient alternative that requires no installation.
What is Java used for?
Java is used for Android apps, enterprise backends, web services, and cloud systems. It powers cross-platform applications including Spring Boot microservices, Android mobile apps, big data tools (Hadoop), and enterprise systems. According to Oracle, Java runs on over 3 billion devices worldwide.

Learn Java the Practical Way

Whether you’re new to programming or expanding your skills, this Java tutorial focuses on hands-on learning. Each topic pairs clear explanations with short, working examples that you can run online. Move from fundamentals—variables, data types, and control flow—to core OOP concepts like classes, inheritance, and interfaces. Along the way, you’ll see idiomatic Java patterns and simple best practices that build confidence.

Why this guide? It’s designed for real-world use: fast to read, easy to try, and friendly on mobile. Bookmark it and return whenever you need a refresher or a quick example.

Java Programming Tutorial — Learn Java Step by Step

Java is one of the most widely-used programming languages in the world, powering Android apps, enterprise backends, cloud services, and scientific computing. This tutorial teaches Java from the ground up with practical, runnable examples you can try in our free Java Compiler or any local IDE.

Each topic includes multiple code examples with explanations, expected output, and best practices. Whether you are a complete beginner or refreshing your knowledge of a specific feature, every page gives you immediately usable code.

What This Tutorial Covers

  • Basics: Hello World, variables, data types, operators
  • Control Flow: if/else, switch, for, while, break/continue
  • Methods: Parameters, return types, overloading, recursion
  • Arrays & Strings: Declaration, manipulation, StringBuilder
  • OOP: Classes, objects, constructors, encapsulation
  • Inheritance: extends, super, abstract classes, final
  • Interfaces: Abstraction, default methods, functional interfaces
  • Collections: ArrayList, HashMap, HashSet, LinkedList
  • Exceptions: try/catch/finally, custom exceptions
  • Generics: Type parameters, bounded types, wildcards
  • Streams (Java 8+): filter, map, reduce, collect
  • File I/O: BufferedReader, Files API, serialisation

Why Learn Java in 2026?

  • Enterprise dominance: Java powers most Fortune 500 backends via Spring Boot and Jakarta EE
  • Android development: Java + Kotlin build apps for 3+ billion active Android devices
  • Job market: Top 3 most-demanded language on LinkedIn, Indeed, and Stack Overflow survey
  • Platform independence: Write once, run anywhere — bytecode runs on any JVM
  • Modern evolution: Java 17/21 LTS added records, sealed classes, virtual threads, pattern matching
  • Ecosystem: Maven/Gradle, IntelliJ IDEA, 100,000+ libraries, massive community

How to Get Started

  1. Run code online: Use our Java Compiler — no installation needed
  2. Install JDK locally: Download OpenJDK 17+ from adoptium.net
  3. Choose an IDE: IntelliJ IDEA Community (free), Eclipse, or VS Code with Java Extension Pack
  4. Follow topics in order: Start from Hello World and progress sequentially
  5. Build a project: After OOP, build a small CRUD app to solidify your knowledge

Frequently Asked Questions

Do I need prior programming experience?

No, zero experience required. This tutorial starts from absolute basics (Hello World, variables) and progresses to advanced topics. Each concept builds on the previous one.

Which Java version should I use?

Java 17 LTS is recommended. All examples in this tutorial work on Java 17+. Our online Java compiler uses the latest stable release.

How long does it take to learn Java?

4–6 weeks for basics, 3–6 months for job-readiness. Basics (syntax, OOP, loops) take 4–6 weeks of daily practice. Intermediate topics (collections, generics, streams) add another month. Full proficiency with frameworks like Spring Boot typically requires 3–6 months total.

Is this tutorial free?

Yes, 100% free. No account, no sign-up, no limits. All topics, examples, and the Java programming examples are available without any restriction.

Who Is This For?

Complete beginners choosing Java as their first language. CS students preparing for university courses and exams. Self-taught developers building a strong OOP foundation. Python/JS developers learning Java for backend or Android work. Interview candidates practising Java data structures. Professional developers needing a quick reference for specific Java features.