Java Syntax Cheat Sheet
Primitive types, reserved keywords, and the most common Java patterns — class declarations, loops, collections — all in one reference with typing practice links.
Primitive Data Types
| Type | Use |
|---|---|
int | Whole numbers |
long | Large whole numbers — suffix L |
double | Decimal numbers (default float type) |
float | Smaller decimal numbers — suffix f |
boolean | Boolean value |
char | Single Unicode character |
byte | Tiny integer |
short | Small integer |
String | Sequence of characters (not primitive) |
Essential Keywords
| Keyword | Meaning |
|---|---|
public | Visible everywhere |
private | Visible only in the declaring class |
protected | Visible in the package and subclasses |
static | Belongs to the class, not an instance |
final | Cannot be changed / overridden / subclassed |
abstract | Class cannot be instantiated / method has no body |
interface | Declare a contract of methods |
implements | Class fulfils an interface contract |
extends | Subclass a parent class |
new | Create an object instance |
this | Reference to the current object |
super | Reference to the parent class |
return | Return a value from a method |
void | Method returns no value |
null | Absence of an object reference |
instanceof | Runtime type check |
try / catch / finally | Exception handling |
throw / throws | Raise or declare exceptions |
synchronized | Thread-safe block or method |
volatile | Always read from main memory (multi-threading) |
enum | Define a fixed set of named constants |
import | Include a class or package |
package | Declare the package namespace |
Common Code Patterns
Class Declaration & Main Method
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}For Loop Variants
// Traditional for loop
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
// Enhanced for loop (for-each)
int[] numbers = {1, 2, 3, 4, 5};
for (int n : numbers) {
System.out.println(n);
}
// While loop
int count = 0;
while (count < 5) {
System.out.println(count++);
}ArrayList Usage
import java.util.ArrayList;
import java.util.List;
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("Go");
for (String lang : languages) {
System.out.println(lang);
}
System.out.println("Size: " + languages.size());
languages.remove("Go");
boolean hasJava = languages.contains("Java");HashMap Usage
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
scores.put("Carol", 92);
// Access
int aliceScore = scores.get("Alice");
scores.putIfAbsent("Dave", 78);
// Iterate
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}Interface & Implementation
public interface Shape {
double area();
double perimeter();
}
public class Circle implements Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
}Practice typing these in CodeSpeedTest →
Java's verbosity means muscle memory matters more than in most languages. Start with Hello World and build up to collections and OOP patterns.