DEVELOPER SANDBOX Academy of Mastery
WelcomeBuild · Explore · Create SD
REFERENCE GUIDE

Java Reference Guide

Syntax • Types • Control Flow • Methods • Objects
Compact Java language reference covering program structure, output, variables, primitive types, strings, operators, arrays, control flow, methods, classes, objects, casting, and common syntax rules.

Java code lives inside classes. A normal application starts in public static void main(String[] args). Statements usually end with semicolons; blocks use braces.

Foundation

01 — Program Structure & Output

Program Structure & Output

Basic Program

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

class Main defines the class. main() is the entry point. System.out.println() writes output.

Program Structure & Output

Printing

System.out.println("Hello");   // print + newline
System.out.print("Hello ");    // print, no newline

String bird = "Heron";
int count = 3;
System.out.println("I painted " + count + " " + bird + " paintings.");

double price = 24.99;
System.out.printf("%s costs $%.2f%n", bird, price);

%s String · %d integer · %f decimal · %.2f two decimal places · %n newline.

Java is statically typed. The core declaration pattern is TYPE name = value;. Primitive values use one of eight primitive types; objects such as String use reference types.

TYPE name = value;

02 — Variables & Types

Variables & Types

Declaration Pattern

int quantity = 5;       // type + name + initial value

int paintings;          // declaration
paintings = 12;         // assignment
paintings = 13;         // change existing value

final int MAX_STUDENTS = 10;  // constant

Do not repeat the type when simply changing an existing variable. final prevents reassignment.

Variables & Types

Eight Primitive Types

byte smallNumber = 10;
short mediumNumber = 1000;
int number = 25;
long hugeNumber = 5000000000L;

float decimal1 = 3.14F;
double decimal2 = 3.14159;

char letter = 'A';
boolean active = true;

Whole numbers: byte, short, int, long. Decimals: float, double. Single character: char. Logic: boolean. long literals commonly use L; float literals use F.

Variables & Types

String Is a Reference Type

String artist = "Susan";
String animal = "Great Blue Heron";
char firstLetter = 'G';

String is a class, not one of the eight primitives. Strings use double quotes; a char uses single quotes.

Variables & Types

Casting & Numeric Conversion

int x = 10;
double y = x;          // widening: automatic

double price = 29.95;
int wholePrice = (int) price;  // narrowing: explicit cast

A narrowing cast may discard information. (int) 29.95 becomes 29.

Java uses familiar arithmetic, comparison, assignment, and Boolean operators. String content is normally compared with .equals(), not ==.

Math • Comparison • Boolean Logic

03 — Operators & Strings

Operators & Strings

Arithmetic & Assignment

+   -   *   /   %

score += 5;
score -= 5;
score *= 2;
score /= 2;
score++;
score--;

Integer division drops the fractional remainder: 10 / 3 is 3. Use decimal operands such as 10.0 / 3.0 for a decimal result.

Operators & Strings

Comparison & Boolean Logic

==   !=   >   <   >=   <=
&&   // AND
||   // OR
!    // NOT

if (age >= 18 && member) {
    System.out.println("Access granted");
}

= assigns a value. == compares values/references.

Operators & Strings

String Methods & Equality

String animal = "Great Blue Heron";

animal.length();
animal.toUpperCase();
animal.toLowerCase();
animal.contains("Heron");

if (animal.equals("Great Blue Heron")) {
    System.out.println("Match");
}

.equals() compares String content. == on objects checks whether references point to the same object.

Use conditional statements to choose behavior, loops to repeat behavior, and arrays to hold multiple values of the same type.

Decide • Repeat • Group Values

04 — Control Flow & Arrays

Control Flow & Arrays

if / else

if (paintings > 10) {
    System.out.println("Large inventory");
} else if (paintings > 5) {
    System.out.println("Medium inventory");
} else {
    System.out.println("Low inventory");
}
Control Flow & Arrays

Arrays

String[] birds = {"Heron", "Egret", "Pelican"};

System.out.println(birds[0]); // Heron
System.out.println(birds.length);

Array indexes start at 0. Arrays use .length; Strings use .length().

Control Flow & Arrays

Loops

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

for (String bird : birds) {
    System.out.println(bird);
}

int count = 0;
while (count < 5) {
    System.out.println(count);
    count++;
}

The enhanced for loop reads as: for each String bird in birds.

Control Flow & Arrays

switch

switch (status) {
    case "available" -> System.out.println("For sale");
    case "sold" -> System.out.println("Sold");
    default -> System.out.println("Unknown");
}

// Traditional form
switch (status) {
    case "sold":
        System.out.println("Sold");
        break;
    default:
        System.out.println("Unknown");
}

Modern Java supports arrow-style switch labels. Older Java code commonly uses colon labels plus break.

A Java method declares what it returns and the types of its parameters. Use void when no value is returned.

Return Type • Name • Parameters

05 — Methods

Methods

Returning a Value

static int add(int a, int b) {
    return a + b;
}

int answer = add(5, 10);

Pattern: returnType methodName(parameters).

Methods

void, Parameters & Arguments

static void greet(String name) {
    System.out.println("Hello " + name);
}

greet("Susan");

String name is a parameter. "Susan" is the argument supplied when calling the method. void means the method returns no value.

A class defines a type. Objects are instances of that type. Constructors initialize new objects.

Create Your Own Types

06 — Classes, Objects & Constructors

Classes, Objects & Constructors

Class & Object

class Painting {
    String title;
    double price;
    boolean sold;
}

Painting painting = new Painting();
painting.title = "Blue Heron";
painting.price = 125.00;
painting.sold = false;

Painting painting follows the same type-first pattern as int count or String title.

Classes, Objects & Constructors

Constructor & this

class Painting {
    String title;
    double price;

    Painting(String title, double price) {
        this.title = title;
        this.price = price;
    }
}

Painting painting = new Painting("Blue Heron", 125.00);

this.title means the title field belonging to the current object.

Classes, Objects & Constructors

Access Modifiers

public class Painting {
    private String title;
    private double price;
}

public = broadly accessible. private = accessible within the class. protected = accessible according to package/subclass rules.

These rules cover several of the mistakes most likely to interrupt early Java work.

Fast Recall

07 — Common Rules & Memory Card

Common Rules & Memory Card

null

String name = null;

// Dangerous if name is still null:
name.length();

null means a reference points to no object. Calling an instance method through a null reference can cause a NullPointerException.

Common Rules & Memory Card

Comments & Naming

// single-line comment

/*
   multi-line comment
*/

/**
 * Javadoc documentation comment
 */

int paintingCount;          // camelCase
void calculatePrice() {}    // camelCase
class StudentRecord {}      // PascalCase
final int MAX_SIZE = 10;    // UPPER_CASE constant
Common Rules & Memory Card

Core Memory Card

// VARIABLE
TYPE name = value;
int count = 10;
double price = 12.50;
boolean active = true;
char grade = 'A';
String name = "Susan";

// PRINT
System.out.println(name);

// CONDITION
if (count > 5) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

// ARRAY + LOOP
String[] birds = {"Heron", "Egret"};
for (String bird : birds) {
    System.out.println(bird);
}

// METHOD
static int add(int a, int b) {
    return a + b;
}

// OBJECT
Painting painting = new Painting();

Five patterns: TYPE name = value; · System.out.println(...); · if (condition) { ... } · for (...) { ... } · returnType methodName(parameters) { ... }