Java Syntax Quick Reference Cheat Sheet
A printable reference covering Java variables, conditionals, loops, methods, classes, collections, exceptions, and modern syntax for grades 10-12.
Related Tools
Related Labs
Related Worksheets
Related Infographics
Study as Flashcards
This Java syntax quick reference covers the core patterns students need when reading and writing Java programs. It is designed to help grades 10-12 students remember the structure of variables, control flow, methods, classes, arrays, and common library features. A compact reference is useful because Java is precise, and small syntax mistakes can prevent a program from compiling. The most important ideas are type declarations, block structure, method calls, object creation, and reusable class design. Students should know how to write if statements, loops, arrays, ArrayLists, constructors, and try-catch blocks. Modern Java also includes helpful features such as var for local variables, enhanced for loops, switch expressions, and lambdas for concise behavior.
Key Facts
- A Java variable declaration uses the pattern type name = value;, such as int count = 5; or String name = "Ada";.
- The main method header is public static void main(String[] args), and it is the usual starting point for a basic Java program.
- An if statement uses if (condition) { statements } else { statements }, and the condition must evaluate to a boolean value.
- A for loop commonly uses for (int i = 0; i < n; i++) { statements } to repeat code a known number of times.
- An enhanced for loop uses for (Type item : collection) { statements } to visit each element in an array or collection.
- A method declaration follows access returnType methodName(parameters) { body }, such as public int add(int a, int b) { return a + b; }.
- An object is created with ClassName obj = new ClassName(arguments);, and instance methods are called with obj.methodName(arguments);.
- Exception handling uses try { riskyCode(); } catch (ExceptionType e) { handleError(); } finally { cleanup(); }.
Vocabulary
- Class
- A class is a blueprint that defines the data fields and methods objects of that type can have.
- Object
- An object is an instance of a class created in memory using the new keyword.
- Method
- A method is a named block of code that can receive parameters, perform actions, and optionally return a value.
- ArrayList
- An ArrayList is a resizable list from java.util that stores objects and provides methods such as add, get, set, and remove.
- Constructor
- A constructor is a special method with the same name as the class that initializes a new object.
- Exception
- An exception is an error or unusual event that interrupts normal program flow and can be handled with try-catch.
Common Mistakes to Avoid
- Forgetting semicolons after statements is wrong because Java uses semicolons to mark the end of most statements, such as int x = 3;.
- Using = instead of == in a condition is wrong because = assigns a value, while == compares two primitive values for equality.
- Comparing Strings with == is wrong because == checks whether two references point to the same object; use str1.equals(str2) to compare text content.
- Using an array or ArrayList index that is too large is wrong because valid indexes run from 0 to length - 1 for arrays and 0 to size() - 1 for ArrayLists.
- Declaring a method with a return type but not returning a value is wrong because every path in a non-void method must return a value of the declared type.
Practice Questions
- 1 Write a Java statement that declares an int variable named score and stores the value 95.
- 2 What does this loop print: for (int i = 1; i <= 4; i++) { System.out.print(i * 2 + " "); }
- 3 Given ArrayList<String> names = new ArrayList<>();, write two statements that add "Mia" and then print the first element.
- 4 Explain when you would use a class with objects instead of writing all code directly inside the main method.