< 1511 Teaching Resources

Syntax Reference

Here you’ll find a quick reference to some basic java syntax.

Built-In Data Types

Variables

type variableName = value;

Assignment:

variableName = newValue;

Example:

String greeting = "Hello World";
double treeHeight = 12.2d; // meters
int funnyNumber = 67;
funnyNumber = 41;          // funnyNumber is now 41

Logic

Operators

If Statements:

if (condition1) {
    // Runs if condition1 is true
} else if (condition2) {
    // Runs if condition1 is false but the 2nd condition is true
} else {
    // Run if condition1 is false and condition2 is false
}

Classes and Functions

Classes need to be named according to the file name and must use a capital letter at the start

class Car {
    private double speed = 0;

    public boolean isMoving() {
        return speed == 0;
    }

    public void accelerate() {
        speed += 1;
    }
    
    public void decelerate() {
        speed -= 1;
    }
}

Functions:

modifiers reutrn_type function_name(argument_1_type arg1, arg_2_type arg2) {
 return return_type;
} 

Ex.:

public double add(double number1, double number2) {
 return number1 + number2;
}