🧠 AI Computer Institute
Content is AI-generated for educational purposes. Verify critical information independently. A bharath.ai initiative.

Java Fundamentals Cheat Sheet

programmingGrades 9-127 sections

Primitive Types

TypeSizeRangeExample
byte8 bits-128 to 127byte b = 10;
short16 bits-32768 to 32767short s = 100;
int32 bits-2^31 to 2^31-1int i = 1000;
long64 bits-2^63 to 2^63-1long l = 1000L;
float32 bits1.4e-45 to 3.4e38float f = 3.14f;
double64 bits4.9e-324 to 1.8e308double d = 3.14;
boolean1 bittrue/falseboolean b = true;
char16 bitsUnicode 0-65535char c = 'A';

Classes & Objects

// Class definition
public class Student {
    // Fields (properties)
    private String name;
    private int age;
    public double gpa;

    // Constructor
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Methods
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Static method (class method)
    public static int getMaxAge() {
        return 25;
    }
}

// Create object
Student s = new Student("Raj", 16);
s.gpa = 3.8;
System.out.println(s.getName());

Inheritance & Polymorphism

// Parent class
public class Animal {
    protected String name;

    public void sound() {
        System.out.println("Some sound");
    }
}

// Child class
public class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("Woof!");
    }
}

// Interface
public interface Drawable {
    void draw();
    void resize();
}

// Implement interface
public class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing circle");
    }

    @Override
    public void resize() {
        System.out.println("Resizing circle");
    }
}

Exception Handling

try {
    int[] arr = new int[5];
    arr[10] = 5;  // ArrayIndexOutOfBoundsException

    int result = 10 / 0;  // ArithmeticException

} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Invalid index: " + e.getMessage());

} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");

} catch (Exception e) {
    // Catches any remaining exception
    System.out.println("General exception: " + e);

} finally {
    System.out.println("Always executes");
}

// Throwing exceptions
public void checkAge(int age) throws IllegalArgumentException {
    if (age < 0) {
        throw new IllegalArgumentException("Age cannot be negative");
    }
}

Collections Framework

import java.util.*;

// List (ordered, allows duplicates)
List list = new ArrayList<>();
list.add(1);
list.add(2);
list.get(0);        // 1
list.size();        // 2
list.remove(0);

// LinkedList (good for frequent insertion/deletion)
List linked = new LinkedList<>();

// Set (unique, unordered)
Set set = new HashSet<>();
set.add(1);
set.add(1);         // Ignored (duplicate)
set.contains(1);    // true

// TreeSet (sorted)
Set sorted = new TreeSet<>();

// Map (key-value)
Map map = new HashMap<>();
map.put("Raj", 95);
map.get("Raj");     // 95
map.remove("Raj");

// Iterate
for (int num : list) {
    System.out.println(num);
}

String Operations

MethodExampleOutput
length()"hello".length()5
charAt()"hello".charAt(1)'e'
substring()"hello".substring(1,4)"ell"
indexOf()"hello".indexOf("l")2
toUpperCase()"hello".toUpperCase()"HELLO"
toLowerCase()"HELLO".toLowerCase()"hello"
trim()" hi ".trim()"hi"
split()"a,b".split(",")["a","b"]
replace()"hello".replace("l","L")"heLLo"

Access Modifiers & Keywords

ModifierClassPackageSubclassAll
publicYesYesYesYes
protectedYesYesYesNo
(default)YesYesNoNo
privateYesNoNoNo

Keywords: static (class-level), final (immutable), abstract (must override), synchronized (thread-safe)

More Cheat Sheets