Code Examples
Short, runnable examples for the basics. Choose a language and open any example in the compiler.
Hello, World!
Every language starts here: print a message to the screen to check that everything works.
Open in compilerpublic class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }Variables
Variables store values such as text, whole numbers and decimals so you can reuse them.
Open in compilerpublic class Main { public static void main(String[] args) { String name = "Ada"; int age = 21; double height = 1.75; System.out.println(name + " " + age + " " + height); } }Conditions
if / else lets your program choose a path depending on a value.
Open in compilerpublic class Main { public static void main(String[] args) { int age = 18; if (age >= 18) { System.out.println("Adult"); } else if (age >= 13) { System.out.println("Teenager"); } else { System.out.println("Child"); } } }Loops
Loops repeat a block of code: use for when you know the count and while when you wait for a condition.
Open in compilerpublic class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println(i); } int n = 3; while (n > 0) { System.out.println("Countdown " + n--); } } }Functions
A function packs code into a reusable block that takes inputs and returns a result.
Open in compilerpublic class Main { static int add(int a, int b) { return a + b; } public static void main(String[] args) { System.out.println(add(2, 3)); } }Fibonacci sequence
Each number is the sum of the two before it. A classic exercise that combines variables, loops and arithmetic.
Open in compilerpublic class Main { public static void main(String[] args) { int a = 0, b = 1; for (int i = 0; i < 10; i++) { System.out.print(a + " "); int next = a + b; a = b; b = next; } } }