Any code you write in Java always lives inside a class. The file name must match the class name.

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

Classes

A class is the basic unit of Java. You define one with the class keyword. The name should start with a capital letter, and the file name must match.

public class MyProgram {
    // code goes here
}

The Main Method

This is the starting point of every Java program. Without it, the code won't run.

public class MyProgram {
    public static void main(String[] args) {
        // program starts here
    }
}

Variables

Variables are very important in Java and programming the robot — they will hold a lot of data required for the robot to run. Java requires you to declare the type of data a variable will hold.

int age = 20;
double price = 9.99;
String name = "Alexander";
boolean isJavaFun = true;

Conditionals

Use if and else to control decisions. The condition inside parentheses must be true/false.

if (height >= 5) {
    System.out.println("Tall");
} else {
    System.out.println("Short");
}

Loops

Loops repeat code. A for loop runs a set number of times. A while loop repeats while a condition is true.

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

while (age < 25) {
    age++;
    // ++ just increases the age by 1
}

Methods

Methods group reusable code. They have a return type, a name, and parentheses for parameters.

public static int add(int a, int b) {
    int c = a + b;
    return c;
}
// if you don't have a return, add void to it

Key Rules

Programming in FTC

To learn more in depth about programming for FTC specifically, you can download this PDF from our GitHub.

You can also check out the tutorial on making your first TeleOp program.