Java syntax is easier to learn when each rule produces an observable compiler or runtime result. This walkthrough starts with a complete Java class, then introduces variables, control flow, methods, collections and exceptions. A beginner should be able to type the examples, compile them, and explain why a particular expression is valid before moving to frameworks.
Install a JDK and create a class
Use a currently supported Java Development Kit (JDK), not only a runtime, because you need javac to compile source files. Save the following as HelloApp.java. The file name matches the public class, and Java identifiers are case sensitive.
public class HelloApp {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
javac HelloApp.java
java HelloApp
# Hello, Java
The compiler creates a class file containing bytecode for the Java Virtual Machine. Source-file launch shortcuts may exist in newer JDKs, but using javac and java separately makes the two steps visible.
Variables, primitive types and reference types
Use int for a simple whole-number example, double for a floating-point value and boolean for a truth test. A String is an object reference, not a primitive. Declared types prevent mixing incompatible values without an explicit conversion.
int units = 3;
double price = 4.5;
boolean available = units > 0;
String item = "Cable";
System.out.println(item + ": " + units * price);
These statements belong inside a method such as main; do not paste them at top level in a regular Java class and expect the same compilation behavior.
Conditionals and loops should have an end condition
for (int i = 1; i <= 3; i++) {
if (i % 2 == 0) {
System.out.println(i + " is even");
} else {
System.out.println(i + " is odd");
}
}
A loop increments i once per iteration and stops when the condition becomes false. Always test the boundaries: one element, zero elements and a limit of three. An off-by-one mistake appears when a developer uses < where <= was intended.
Methods, arrays and collections
Methods name reusable behavior and document their inputs and outputs through parameter and return types. Arrays have a fixed length; an ArrayList can grow. Use a collection when the number of elements changes during execution, and do not confuse list indexes with one-based item numbers.
import java.util.ArrayList;
public class Totals {
static int square(int n) { return n * n; }
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>();
values.add(2);
values.add(4);
for (int value : values) {
System.out.println(square(value));
}
}
}
Common compiler errors and what they mean
A missing semicolon can cause a syntax error; a misspelled type or method produces a symbol-resolution error; and invoking an instance method from a static context without an object causes a static-context error. Read the first compiler diagnostic and the indicated source line before changing multiple lines. Do not suppress warnings by adding arbitrary casts.
Exceptions are not a substitute for validation
Use try/catch for operations that can fail after validation, such as parsing an external value. Catch a specific exception, explain what failed, and keep the original cause available for diagnosis. Avoid swallowing every exception with an empty catch block.
String input = "12";
try {
int amount = Integer.parseInt(input);
System.out.println(amount * 2);
} catch (NumberFormatException ex) {
System.err.println("Not a valid integer: " + input);
}
Practice with small tests before a framework
Change the input, predict the output and rerun the compiler. Then package the same logic into a named method with small unit tests. After syntax and collections are comfortable, learn build tools, modules and common APIs. For the precise compiler options and source-file expectations, refer to Oracle’s javac documentation.