Java Cheat Sheet
1. Data Types & Variables
Primitive Types: byte, short, int, long, float, double, char, boolean
Reference Types: String, Arrays, Classes, Interfaces
Declaration: int x = 10;
Type Casting: int a = (int) 5.5;
2. Operators
Arithmetic: + - * / %
Relational: == != > < >= <=
Logical: && || !
Bitwise: &, |, ^, ~, <<, >>, >>>
Assignment: = += -= *= /= %=
Ternary: condition ? true : false
3. Control Flow
if, if-else, if-else-if
switch-case (with break/default)
Loops: for, while, do-while
Branching: break, continue, return
4. Arrays
int[] arr = new int[5];
int[] arr = {1, 2, 3};
for (int i : arr) { ... }
Arrays class: [Link](arr);
5. Methods
returnType methodName(params) { ... }
Overloading: multiple methods with same name but different params
Static Methods: static int add(int a, int b)
6. Object-Oriented Programming
Class: blueprint for objects
Object: instance of class
Constructor: special method to initialize objects
this: refers to current object
new: used to create objects
7. Inheritance
class A { ... }
class B extends A { ... }
super: calls superclass constructor/method
Method Overriding: subclass redefines superclass method
Java Cheat Sheet
8. Abstraction & Interfaces
Abstract class: contains abstract & concrete methods
abstract class A { abstract void show(); }
Interface: all methods are abstract by default
interface I { void run(); }
9. Encapsulation & Polymorphism
Encapsulation: hiding data using private fields + getters/setters
Polymorphism: same method name, different behaviors (overload/override)
10. Exception Handling
try { ... } catch(Exception e) { ... }
finally { ... }
throws, throw
Custom Exceptions: class MyEx extends Exception
11. Collections Framework
List, Set, Map, Queue
ArrayList<String> list = new ArrayList<>();
HashMap<K,V>, HashSet<E>
Iterate using for-each or Iterator
12. File Handling
FileReader, BufferedReader, FileWriter, BufferedWriter
try-with-resources
File file = new File("[Link]");
[Link](), [Link]()
13. Multithreading
Thread class or Runnable interface
Thread t = new Thread(new MyRunnable());
[Link]();
synchronized blocks, sleep(), join()
14. Java 8 Features
Lambda: (a, b) -> a + b
Stream API: [Link]().filter(...).collect(...)
Functional Interfaces: Runnable, Comparator, etc.
Default & Static methods in interfaces