Java asks for more structure up front than Python or JavaScript. Once you know what each part is for, the ceremony stops feeling arbitrary.
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 namedHello.javato 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:
javac Hello.java # produces Hello.class
java Hello # runs it — no .class on the endVariables and types#
Java is statically typed: you state what a variable holds and it cannot hold anything else.
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 intThe 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#
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#
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#
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#
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#
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.