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.
What is Java?
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.
Your First Program: Hello World
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:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Click to see the output
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 formainfirst.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.
"Hello, Alex!" and imagine what would print.Variables & Data Types
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."
| Type | Holds | Example |
|---|---|---|
int | whole numbers | int age = 12; |
double | decimal numbers | double price = 4.99; |
boolean | true or false | boolean isFun = true; |
char | a single character | char grade = 'A'; |
String | text | String name = "Maya"; |
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
int level = 1; can never hold text later — that's a type mismatch, and Java will refuse to run.Operators
Operators let you do math and ask questions about values.
| Kind | Symbols | Example | Result |
|---|---|---|---|
| Arithmetic | + - * / % | 7 % 2 | 1 (remainder) |
| Comparison | == != > < >= <= | 5 > 3 | true |
| Logical | && || ! | true && false | false |
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
= means "store this value" (assignment), while == means "are these equal?" (comparison). Mixing them up is the #1 beginner mistake.Making Decisions: if / else
Programs need to make choices. if lets your code do different things depending on whether something is true.
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
Java checks each condition top to bottom and runs the first one that's true. If none match, it falls into else.
Repeating Things: Loops
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).
// 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
Arrays
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.
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
Methods (Reusable Instructions)
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.
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
Break down a method's parts:
intbefore 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.
cube that returns number * number * number, then call cube(3) in your head — what comes back?Classes & Objects
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.
public class Dog { String name; int age; public void bark() { System.out.println(name + " says Woof!"); } }
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
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.
int age = 12;+ - == &&int[] anew