A from-scratch, syllabus-mapped walkthrough of Java โ from your very first Hello World to full object-oriented programs.
Every Java program lives inside a class, and every runnable program needs exactly one main method โ that's where execution starts.
Save this as Main.java, then compile it with javac Main.java and run it with java Main.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Java is statically typed โ you declare the type of every variable before you use it. The IEB syllabus focuses on int, double, boolean, char and String.
int age = 17;
double average = 84.5;
boolean passed = true;
char grade = 'A';
String name = "Amahle";
System.out.println(name + " is " + age + " and scored " + average);Scanner reads from the keyboard (System.in). Always import it, and match the read method to the type you expect.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your mark: ");
int mark = sc.nextInt();
System.out.println("You scored " + mark);
}
}Arithmetic (+ - * / %), relational (== != < > <= >=) and logical (&& || !) operators are the backbone of every algorithm question.
int a = 17, b = 5;
System.out.println(a / b); // 3 (integer division โ the remainder is dropped)
System.out.println(a % b); // 2 (modulus โ the remainder)
System.out.println(a / (double) b); // 3.4 โ casting forces a decimal answer
boolean canVote = (a >= 18) && (b > 0);Selection statements let a program choose between different paths depending on a condition.
int mark = 72;
if (mark >= 80) {
System.out.println("Distinction");
} else if (mark >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
// switch works well for a fixed set of exact values
char grade = 'B';
switch (grade) {
case 'A': System.out.println("Excellent"); break;
case 'B': System.out.println("Good"); break;
default: System.out.println("Keep working"); break;
}Use for when you know how many times to repeat, while when you don't, and do-while when the loop body must run at least once.
// for โ counting loop
for (int i = 1; i <= 5; i++) {
System.out.println("Line " + i);
}
// while โ condition checked first
int n = 10;
while (n > 0) {
n = n / 2;
}
// do-while โ body always runs once
int choice;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter 0 to quit: ");
choice = sc.nextInt();
} while (choice != 0);An array holds a fixed number of values of the same type, accessed by index starting at 0.
int[] marks = {78, 65, 91, 55, 88};
int total = 0;
for (int i = 0; i < marks.length; i++) {
total += marks[i];
}
double average = total / (double) marks.length;
System.out.println("Average: " + average);A method groups reusable logic behind a name. static methods belong to the class itself and don't need an object to call them.
public class Main {
public static void main(String[] args) {
System.out.println(square(6));
}
static int square(int n) {
return n * n;
}
}A class is a blueprint; an object is an instance built from it. Fields store an object's state, methods define its behaviour.
This is the foundation of the IEB Grade 12 OOP section โ cycle tests almost always start here.
public class Learner {
String name;
int mark;
void printReport() {
System.out.println(name + " scored " + mark);
}
}
public class Main {
public static void main(String[] args) {
Learner l = new Learner();
l.name = "Sipho";
l.mark = 72;
l.printReport();
}
}A constructor initialises an object when it's created. Encapsulation means keeping fields private and only exposing them through getters/setters โ protecting the object's internal state.
public class Learner {
private String name;
private int mark;
public Learner(String name, int mark) {
this.name = name;
this.mark = mark;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
if (mark >= 0 && mark <= 100) {
this.mark = mark;
}
}
}A subclass (extends) reuses and extends a superclass's fields and methods โ modelling an "is-a" relationship.
public class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public void greet() {
System.out.println("Hi, I'm " + name);
}
}
public class Learner extends Person {
private int grade;
public Learner(String name, int grade) {
super(name);
this.grade = grade;
}
}A subclass can override a method to give it a more specific implementation. The correct version runs automatically based on the object's real type.
public class Person {
public void greet() {
System.out.println("Hello.");
}
}
public class Learner extends Person {
@Override
public void greet() {
System.out.println("Hey, I'm a learner!");
}
}
public class Main {
public static void main(String[] args) {
Person p = new Learner(); // declared as Person, actually a Learner
p.greet(); // prints "Hey, I'm a learner!"
}
}try/catch lets your program recover from runtime errors โ like bad user input โ instead of crashing.
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println(100 / n);
} catch (ArithmeticException e) {
System.out.println("Can't divide by zero!");
} catch (Exception e) {
System.out.println("Something went wrong: " + e.getMessage());
}Ready to put it into practice?
Every technique above shows up in the Coding Arena problem set.
Try the Coding Arena โ