Skip to content

Java

Hello World

HelloWorld.java
1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Variables and Types

VariablesAndTypes.java
1
2
3
4
5
int a = 10;
double b = 5.5;
boolean isValid = true;
char letter = 'A';
String name = "Alice";

Control Flow

ControlFlow.java
1
2
3
4
5
if (a > 0) {
    System.out.println("Positive");
} else {
    System.out.println("Non-positive");
}
ControlFlow.java
1
2
3
4
5
switch (day) {
    case 1 -> System.out.println("Monday");
    case 2 -> System.out.println("Tuesday");
    default -> System.out.println("Other day");
}
ControlFlow.java
1
2
3
4
5
6
7
8
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

int j = 0;
while (j < 5) {
    System.out.println(j++);
}

Arrays

Arrays.java
int[] nums = {1, 2, 3};
System.out.println(nums[0]);
Arrays.java
1
2
3
4
5
int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6}
};
System.out.println(matrix[0][1]);
Arrays.java
1
2
3
4
int[] nums = new int[5];
for (int i = 0; i < nums.length; i++) {
    nums[i] = i * 2;
}
Arrays.java
1
2
3
for (int n : nums) {
    System.out.println(n);
}

Classes and Objects

ClassesAndObjects.java
public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public void greet() {
        System.out.println("Hi, I'm " + name);
    }
}
ClassesAndObjects.java
1
2
3
4
5
6
public class Main {
    public static void main(String[] args) {
        Person p = new Person("Alice");
        p.greet();
    }
}
ClassesAndObjects.java
1
2
3
4
5
public class Animal {
    public void makeSound() {
        System.out.println("Animal sound");
    }
}

Inheritance and Polymorphism

Inheritance.java
1
2
3
4
5
public class Animal {
    public void speak() {
        System.out.println("Sound");
    }
}
Inheritance.java
1
2
3
4
5
6
public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}
Inheritance.java
1
2
3
4
5
6
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound(); // Output: Bark
    }
}

Interfaces and Abstract Classes

Interfaces.java
1
2
3
public interface Flyable {
    void fly();
}
Interfaces.java
1
2
3
4
5
6
public class Bird implements Flyable {
    @Override
    public void fly() {
        System.out.println("Flapping wings");
    }
}
Interfaces.java
1
2
3
public abstract class Shape {
    public abstract double area();
}

Exception Handling

ExceptionHandling.java
1
2
3
4
5
try {
    int x = 5 / 0;
} catch (ArithmeticException e) {
    System.out.println("Divide by zero!");
}
ExceptionHandling.java
1
2
3
4
5
public class MyException extends Exception {
    public MyException(String msg) {
        super(msg);
    }
}
ExceptionHandling.java
1
2
3
4
5
try {
    throw new MyException("Custom exception");
} catch (MyException e) {
    System.out.println(e.getMessage());
}

Collections

Collections.java
1
2
3
4
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list.get(0));
Collections.java
1
2
3
Map<String, Integer> map = new HashMap<>();
map.put("age", 30);
System.out.println(map.get("age"));

Lambdas and Streams

Lambdas.java
List<String> names = List.of("Bob", "Alice", "Zoe");
names.stream().sorted().forEach(System.out::println);
Lambdas.java
1
2
3
4
5
List<Integer> nums = List.of(1, 2, 3, 4, 5);
nums.stream()
    .filter(n -> n % 2 == 0)
    .map(n -> n * n)
    .forEach(System.out::println);

File I/O

Java 11+

FileIO.java
String content = Files.readString(Path.of("file.txt"));
System.out.println(content);
FileIO.java
Files.writeString(Path.of("out.txt"), "Hello File!");

Records

Records.java
public record Person(String name, int age) {}

Threads

Threads.java
1
2
3
4
5
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running...");
    }
}
Threads.java
Runnable r = () -> System.out.println("Lambda thread");
new Thread(r).start();