Skip to content
Happy Programming Guide
Start learning
Java

Java Basics for Beginners

Java’s structure, types, control flow and the class-and-main-method boilerplate — explained so the ceremony makes sense rather than getting in the way.

A pen and an open notebook ready for notes

Java asks for more structure up front than Python or JavaScript. Once you know what each part is for, the ceremony stops feeling arbitrary.

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

Every Java program starts here, so it is worth decoding before anything else.

Decoding the boilerplate#

  • public class Hello — all Java code lives inside a class. The file must be named Hello.java to match.
  • public — anything can call this.
  • static — belongs to the class itself, so it runs without creating an object first.
  • void — returns nothing.
  • main — the name Java looks for to start a program.
  • String[] args — anything typed after the program name on the command line.

Compile and run:

Terminal
javac Hello.java     # produces Hello.class
java Hello           # runs it — no .class on the end

Variables and types#

Java is statically typed: you state what a variable holds and it cannot hold anything else.

JAVA
int count = 7;
double price = 19.99;
boolean isOpen = true;
char grade = 'A';               // single quotes
String name = "Ada";            // double quotes

final int MAX = 100;            // cannot be reassigned
var total = count * 2;          // Java 10+, type inferred as int

The strictness is the point. A mismatch is caught when you compile rather than when a user hits it.

Type Holds
int Whole numbers
long Bigger whole numbers
double Decimals
boolean true / false
char One character
String Text (an object, not a primitive)

Conditions#

JAVA
int marks = 74;

if (marks >= 80) {
    System.out.println("Distinction");
} else if (marks >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Try again");
}

String grade = marks >= 50 ? "Pass" : "Fail";

Loops#

JAVA
for (int i = 0; i < 5; i++) {
    System.out.println(i);              // 0 1 2 3 4
}

String[] fruits = {"apple", "banana"};
for (String fruit : fruits) {           // enhanced for — use this by default
    System.out.println(fruit);
}

int n = 3;
while (n > 0) {
    System.out.println(n);
    n--;
}

Arrays and lists#

JAVA
int[] numbers = new int[5];            // fixed size, all zeros
String[] names = {"Ada", "Sam"};       // fixed size, initialised

System.out.println(names.length);      // .length — no brackets

// Growable, and what you will use most
import java.util.ArrayList;
import java.util.List;

List<String> items = new ArrayList<>();
items.add("pen");
items.add("book");
System.out.println(items.size());      // .size() — with brackets
System.out.println(items.get(0));

Arrays use .length, lists use .size(), and strings use .length(). That inconsistency catches everyone.

Methods#

JAVA
public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));    // 5
    }
}

Every method states its return type, or void for none.

Reading input#

JAVA
import java.util.Scanner;

Scanner scanner = new Scanner(System.in);
System.out.print("Your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
scanner.close();

Questions people ask#

Why must the file name match the class?

For public classes Java requires it, so the compiler can find a class from its name without scanning every file.

What is the difference between int and Integer?

int is a primitive holding a value directly. Integer is an object wrapper, which is what collections like List require. Java converts between them automatically, which is convenient and occasionally the cause of a surprising NullPointerException.

Do I need an IDE?

Not to start — a text editor and the javac/java commands are enough and teach you what is happening. IntelliJ IDEA or Eclipse become worth it once projects have several files.

Why so much typing compared to Python?

Java trades brevity for checks the compiler can make before the program runs. On a large codebase with many people that trade is often worth it.

Where to go next#

Next lessonObject-oriented programming in Java

Keep reading

Web Development

CSS Flexbox Explained

Flexbox lays items out in one direction. Once you understand the main axis, justify-content and align-items stop being guesswork — here is…

4 min read

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *