Skip to main content
eLearner.app

Java Course

Cheatsheet

A quick reference — the essential syntax of modern Java on a single page. Press Ctrl/Cmd + P to print it.

Java · Cheatsheet — eLearner.app

Variables and types

  • Declaration and Assignment

    int age = 25;
    double price = 19.99;
    boolean isActive = true;
    char grade = 'A';
    String name = "Alice";

    Java is strongly typed: every variable must explicitly declare its type.

  • Casting and Conversions

    double d = 9.8;
    int i = (int) d;               // 9 (narrowing cast)
    int num = Integer.parseInt("42");
    String s = String.valueOf(123);

    Casting from float/double to int truncates decimal values without rounding.

Strings

  • Common Methods

    String s = "Hello";
    int len = s.length();          // 5
    char c = s.charAt(0);          // 'H'
    String sub = s.substring(1, 4); // "ell"
    boolean eq = s.equals("hello");// false
    boolean eqIgnoreCase = s.equalsIgnoreCase("hello"); // true

    Always use .equals() method to compare string values, not the == operator.

  • Concatenation and Formatting

    String name = "Bob";
    int age = 30;
    String msg = "Name: " + name + ", Age: " + age;
    String formatted = String.format("Name: %s, Age: %d", name, age);
    System.out.printf("Price: %.2f", 19.99);

Control Flow

  • Conditional Branching

    if (x > 0) {
        System.out.println("Positive");
    } else if (x < 0) {
        System.out.println("Negative");
    } else {
        System.out.println("Zero");
    }
  • Loops (for, foreach, while)

    for (int i = 0; i < 5; i++) {
        System.out.println(i);
    }
    
    for (String item : items) {
        System.out.println(item);
    }
    
    while (x > 0) {
        x--;
    }

    The foreach loop (for-each) allows easy iteration over arrays or collections.

Classes and Objects (OOP)

  • Class and Constructor

    class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public void introduce() {
            System.out.println("Hello, " + name);
        }
    }

    Private members (private) are not directly accessible from outside the class.

  • Inheritance

    class Dog extends Animal {
        public Dog(String name) {
            super(name); // calls parent constructor
        }
    
        @Override
        public void makeSound() {
            System.out.println("Woof");
        }
    }

    Use the @Override annotation to explicitly indicate that a method is overriding a parent method.

  • Abstract Classes

    abstract class Shape {
        abstract double getArea();
    }
    
    class Circle extends Shape {
        double radius;
        @Override
        double getArea() {
            return Math.PI * radius * radius;
        }
    }

Interfaces and Collections

  • Interfaces

    interface Drivable {
        void drive();
    }
    
    class Car implements Drivable {
        @Override
        public void drive() {
            System.out.println("Driving a car");
        }
    }

    A class can implement multiple interfaces, separated by commas.

  • ArrayList (Dynamic List)

    import java.util.ArrayList;
    
    ArrayList<String> list = new ArrayList<>();
    list.add("Java");
    list.add("Kotlin");
    String first = list.get(0);    // "Java"
    int size = list.size();        // 2
    list.remove(1);                // Removes "Kotlin"

    ArrayLists only support object types (e.g., ArrayList<Integer>, not ArrayList<int>).

eLearner.app · Java Course · cheatsheet generated from lesson content.