Java Quick Reference Guide
Basic Syntax Methods
public class HelloWorld { public static int add(int a, int b) {
public static void main(String[] args) { return a + b;
[Link]("Hello, World!"); }
} Classes & Objects
public class Car {
Primitive Data Types private String color;
byte (8-bit), short (16-bit), int (32-bit), long (64-bit), float (32-bit), public Car(String c) { color = c; }
double (64-bit), boolean, char public String getColor() { return color; }
}
Variables Car myCar = new Car("red");
int age = 25; // Integer
double price = 19.99; // Decimal Inheritance
String name = "John"; // String public class Dog extends Animal {
final int MAX = 100; // Constant @Override
public void sound() { [Link]("Bark");
Operators }
Arithmetic: +, -, *, /, %, ++, -- }
Comparison: ==, !=, >, <, >=, <=
Logical: &&, ||, ! Interface
public interface Drawable {
Control Flow void draw();
if (condition) { } else if { } else { } }
switch(var) { case 1: break; default: } class Circle implements Drawable { }
for (int i=0; i<10; i++) { }
while (condition) { } Exception Handling
do { } while (condition); try {
// code
Arrays } catch (Exception e) {
int[] numbers = {1, 2, 3, 4, 5}; [Link]();
String[] names = new String[3]; } finally {
[Link] // Array size // cleanup
Collections
ArrayList<String> list = new ArrayList<>();
HashMap<String, Integer> map = new HashMap<>();
HashSet<Integer> set = new HashSet<>();