| Type | Size | Range | Example |
|---|---|---|---|
| byte | 8 bits | -128 to 127 | byte b = 10; |
| short | 16 bits | -32768 to 32767 | short s = 100; |
| int | 32 bits | -2^31 to 2^31-1 | int i = 1000; |
| long | 64 bits | -2^63 to 2^63-1 | long l = 1000L; |
| float | 32 bits | 1.4e-45 to 3.4e38 | float f = 3.14f; |
| double | 64 bits | 4.9e-324 to 1.8e308 | double d = 3.14; |
| boolean | 1 bit | true/false | boolean b = true; |
| char | 16 bits | Unicode 0-65535 | char c = 'A'; |
Java Fundamentals Cheat Sheet
Primitive Types
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
| Method | Example | Output |
|---|---|---|
| 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
| Modifier | Class | Package | Subclass | All |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| (default) | Yes | Yes | No | No |
| private | Yes | No | No | No |
Keywords: static (class-level), final (immutable), abstract (must override), synchronized (thread-safe)