Code Brew
A 1-hour Java adventure

Learn Java in 60 minutes

Java is the language behind Minecraft mods, Android apps, and tons of real software. This guide walks through the ideas that show up in almost every Java program — with real code you can type out and try.

9lessons
~60 mintotal time
0installs needed to start reading
1

What is Java?

3 min

Java is a programming language — a set of instructions a computer can follow, written in a way that's stricter than English but still readable. The name comes from coffee (the developers were drinking a lot of it), and the logo is a coffee cup.

Think of a Java program like a recipe: it's a list of exact steps, done in order, with no guessing allowed. If a recipe says "add 2 eggs," you can't add "some eggs" — Java is the same way. It wants exact instructions.

Why Java? It's used to build Android apps, Minecraft (the original Java Edition!), banking systems, and school software. Learning it teaches habits that carry over to almost any other language.
2

Your First Program: Hello World

5 min

Every Java program lives inside a class, and every program starts running from a special method called main. Here's the smallest complete Java program:

HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Click to see the output
Hello, World!

Let's break that down line by line:

  • public class HelloWorld — every file needs a class, and the class name must match the filename (HelloWorld.java).
  • public static void main(String[] args) — this is the "start here" signal. Java always looks for main first.
  • System.out.println("...") — prints text to the screen, then moves to a new line.
  • The curly braces { } mark where a block of code begins and ends. The semicolon ; ends an instruction, like a period ends a sentence.
Try it: Change the text between the quotes to your own name, like "Hello, Alex!" and imagine what would print.
3

Variables & Data Types

8 min

A variable is a labeled box that stores a value. In Java, every box has to say up front what type of thing it will hold — that's what makes Java "strict."

TypeHoldsExample
intwhole numbersint age = 12;
doubledecimal numbersdouble price = 4.99;
booleantrue or falseboolean isFun = true;
chara single characterchar grade = 'A';
StringtextString name = "Maya";
Player.java
int level = 1;
double health = 100.0;
boolean hasSword = false;
String playerName = "Ash";

System.out.println(playerName + " is level " + level);
Click to see the output
Ash is level 1
Good to know: Once you set a variable's type, it can't change. int level = 1; can never hold text later — that's a type mismatch, and Java will refuse to run.
4

Operators

6 min

Operators let you do math and ask questions about values.

KindSymbolsExampleResult
Arithmetic+ - * / %7 % 21 (remainder)
Comparison== != > < >= <=5 > 3true
Logical&& || !true && falsefalse
Score.java
int score = 85;
int bonus = 10;
int total = score + bonus;
boolean passed = total >= 90;

System.out.println("Total: " + total);
System.out.println("Passed: " + passed);
Click to see the output
Total: 95 Passed: true
Watch out: = means "store this value" (assignment), while == means "are these equal?" (comparison). Mixing them up is the #1 beginner mistake.
5

Making Decisions: if / else

8 min

Programs need to make choices. if lets your code do different things depending on whether something is true.

score >= 90? true or false? print "Grade: A" print "Keep going" true false
A decision splits the program into two paths — only one runs.
Grade.java
int score = 85;

if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 80) {
    System.out.println("Grade: B");
} else {
    System.out.println("Keep going!");
}
Click to see the output
Grade: B

Java checks each condition top to bottom and runs the first one that's true. If none match, it falls into else.

6

Repeating Things: Loops

8 min

Loops repeat a block of code so you don't have to copy-paste it. The two you'll use most: for (when you know how many times) and while (when you don't).

i = 1 i <= 5? print i stop true false i = i + 1, then check again
A loop keeps checking and repeating until the condition becomes false.
CountUp.java
// for loop: runs a known number of times
for (int i = 1; i <= 5; i++) {
    System.out.println("Round " + i);
}

// while loop: runs until a condition becomes false
int lives = 3;
while (lives > 0) {
    System.out.println("Lives left: " + lives);
    lives--;
}
Click to see the output
Round 1 Round 2 Round 3 Round 4 Round 5 Lives left: 3 Lives left: 2 Lives left: 1
i++ means "add 1 to i." lives-- means "subtract 1 from lives." They're shortcuts you'll see constantly.
7

Arrays

6 min

An array is a row of boxes that all hold the same type, so you can group related values under one name instead of making a new variable for each one.

scores[ ] 90 85 77 92 [0] [1] [2] [3]
Arrays are numbered starting at 0, not 1 — the first box is index 0.
Scores.java
int[] scores = {90, 85, 77, 92};

System.out.println(scores[0]);      // first item
System.out.println(scores.length); // how many items

for (int i = 0; i < scores.length; i++) {
    System.out.println("Score " + i + ": " + scores[i]);
}
Click to see the output
90 4 Score 0: 90 Score 1: 85 Score 2: 77 Score 3: 92
8

Methods (Reusable Instructions)

8 min

A method is a named block of code you can run whenever you need it, instead of retyping the same steps. You've already used one: main.

MathHelper.java
public static int square(int number) {
    return number * number;
}

public static void main(String[] args) {
    int result = square(5);
    System.out.println("5 squared is " + result);
}
Click to see the output
5 squared is 25

Break down a method's parts:

  • int before the name — the type of value it hands back (its return type).
  • square — the method's name, chosen by you.
  • (int number) — the parameter: input the method needs to do its job.
  • return number * number; — the answer sent back to whoever called it.
Try it: Write a method called cube that returns number * number * number, then call cube(3) in your head — what comes back?
9

Classes & Objects

8 min

A class is a blueprint. An object is something built from that blueprint. One cookie cutter (class) can stamp out many cookies (objects) — each cookie can be decorated differently, but they all share the same shape.

class Dog String name int age bark() "Rex" age = 3 "Milo" age = 1 "Bella" age = 5
One class, many objects — each with its own values for the same fields.
Dog.java
public class Dog {
    String name;
    int age;

    public void bark() {
        System.out.println(name + " says Woof!");
    }
}
Main.java
Dog rex = new Dog();
rex.name = "Rex";
rex.age = 3;
rex.bark();

Dog milo = new Dog();
milo.name = "Milo";
milo.bark();
Click to see the output
Rex says Woof! Milo says Woof!

name and age are fields — data the object stores. bark() is a method that belongs to every Dog. new Dog() stamps out a brand-new object from the blueprint.

Cheat Sheet

The nine ideas from today, in one glance.

Variablea labeled box: int age = 12;
Operatormath or comparison: + - == &&
if / elsechoose a path based on true/false
for looprepeat a known number of times
while looprepeat until a condition is false
Arraya row of same-type boxes: int[] a
Methoda reusable, named block of steps
Classa blueprint for building objects
Objectone thing built with new