Main Method
public static void main(String[] args) {
...
}The entry point of a Java program.
Java syntax quick reference: variables, loops, collections, classes and exceptions, from Oracle's official dev.java docs. Every item below is clearly marked as a shortcut, command, or syntax reference.
public static void main(String[] args) {
...
}The entry point of a Java program.
System.out.println("Hello");Prints a line to standard output.
int count = 0;
String name = "Ada";Declares a typed variable.
if (x > 0) {
...
} else if (x == 0) {
...
} else {
...
}Runs code depending on a condition.
for (int i = 0; i < 10; i++) {
...
}Loops a set number of times.
for (String item : items) {
...
}Loops over every item in an array or collection.
int[] nums = {1, 2, 3};
int[] empty = new int[5];Creates a fixed-size array.
List<String> names = new ArrayList<>();Creates a resizable list.
The entry point of a Java program.
public static void main(String[] args) {
...
}Prints a line to standard output.
System.out.println("Hello");Declares a typed variable.
int count = 0;
String name = "Ada";Runs code depending on a condition.
if (x > 0) {
...
} else if (x == 0) {
...
} else {
...
}Loops a set number of times.
for (int i = 0; i < 10; i++) {
...
}Loops over every item in an array or collection.
for (String item : items) {
...
}Creates a fixed-size array.
int[] nums = {1, 2, 3};
int[] empty = new int[5];Creates a resizable list.
List<String> names = new ArrayList<>();Creates key-value pairs.
Map<String, Integer> ages = new HashMap<>();
ages.put("Ada", 36);Compares string contents (not references).
a.equals(b)Defines a method with parameters and a return type.
public int add(int a, int b) {
return a + b;
}Defines a class with a field and constructor.
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
}Handles exceptions.
try {
...
} catch (IOException e) {
...
} finally {
...
}Declares a value that can't change.
static final int MAX = 100;Gets the size of an array.
nums.lengthAdds an item to a list.
names.add("Ada");Common string operations.
s.length() s.toUpperCase() s.trim() s.contains("x") s.split(",")Converts a string to a number.
int n = Integer.parseInt("42");Creates an instance of a class.
Dog d = new Dog("Rex");Writes a short anonymous function.
names.forEach(n -> System.out.println(n));Compiles and runs a Java file from the terminal.
javac Main.java
java MainPrints a formatted string.
System.out.printf("%s is %d%n", name, age);Single-line, block and Javadoc comments.
// line
/* block */
/** Javadoc */Lets the compiler infer a local variable's type.
var list = new ArrayList<String>();Java's built-in primitive types.
byte short int long float double boolean charPicks one of two values in one line.
String s = ok ? "yes" : "no";Returns a value based on cases.
String day = switch (n) {
case 1 -> "Mon";
case 2 -> "Tue";
default -> "?";
};Repeats while a condition is true.
while (x < 10) {
x++;
}Gets the number of items in a collection.
names.size()Inherits from another class.
class Puppy extends Dog { ... }Defines and implements an interface.
interface Shape { double area(); }
class Circle implements Shape { ... }Transforms a collection with streams.
list.stream().filter(x -> x > 0).map(x -> x * 2).toList();Imports a class from a package.
import java.util.List;Creates an unmodifiable list.
List<Integer> nums = List.of(1, 2, 3);Returns a value or a default if the key is missing.
ages.getOrDefault("Bob", 0)Loops over keys and values.
for (var e : ages.entrySet()) {
e.getKey(); e.getValue();
}Joins strings with a separator.
String.join(", ", names)Defines an immutable data class in one line.
record Point(int x, int y) {}Defines a fixed set of constants.
enum Color { RED, GREEN, BLUE }Throws an exception on purpose.
throw new IllegalArgumentException("bad");It covers 40 common Java patterns and examples, including Main Method, Print to Console, Declare a Variable, If / Else, For Loop.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.