Skip to content

Java

Hello World program

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Variables and constants

int x = 10;
final int y = 20;

Data types

int i = 10;
long l = 100000L;
float f = 1.23f;
double d = 3.14159;
boolean b = true;
char c = 'a';
String s = "Hello, world!";

Operators

int sum = x + y;
int difference = x - y;
int product = x * y;
int quotient = x / y;
int remainder = x % y;
boolean isEqual = x == y;
boolean isNotEqual = x != y;
boolean isGreaterThan = x > y;
boolean isLessThan = x < y;
boolean isGreaterThanOrEqual = x >= y;
boolean isLessThanOrEqual = x <= y;
boolean andOperator = x > y && x < 20;
boolean orOperator = x > y || y < 20;
boolean notOperator = !(x > y);

Arrays

int[] numbers = {1, 2, 3, 4, 5};
int[] moreNumbers = new int[5];
moreNumbers[0] = 1;
moreNumbers[1] = 2;
moreNumbers[2] = 3;
moreNumbers[3] = 4;
moreNumbers[4] = 5;

Loops

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
while (x > y) {
    System.out.println("x is greater than y");
    x--;
}
do {
    System.out.println("x is greater than y");
    x--;
} while (x > y);

Conditional statements

if (x > y) {
    System.out.println("x is greater than y");
} else if (x < y) {
    System.out.println("x is less than y");
} else {
    System.out.println("x is equal to y");
}

Switch statement

int dayOfWeek = 1;
switch (dayOfWeek) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    case 4:
        System.out.println("Thursday");
        break;
    case 5:
        System.out.println("Friday");
        break;
    case 6:
        System.out.println("Saturday");
        break;
    case 7:
        System.out.println("Sunday");
        break;
    default:
        System.out.println("Invalid day of week");
        break;
}

Methods

public int add(int x, int y) {
    return x + y;
}

Classes and objects

public class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}
Person p = new Person("John", 30);
System.out.println(p.getName());
System.out.println(p.getAge());

Inheritance

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}
class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}
Dog d = new Dog();
d.eat();
d.bark();

Packages

package com.example;
import java.util.ArrayList;

Exception handling

try {
    int x = 1 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero.");
}

File I/O

import java.io.*;
try {
    FileWriter writer = new FileWriter("example.txt");
    writer.write("Hello, world!");
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}
try {
    FileReader reader = new FileReader("example.txt");
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();
} catch (IOException e) {
    e.printStackTrace();
}