The Basics
Variables, data types, operators, scope, and writing code other people can read.
Variables
A variable is a named container for a value. You give it a type, a name, and a starting value. In FRC you use variables constantly — motor IDs, speeds, sensor readings, on/off flags. Naming them well is half the job.
// type name = value; int motorID = 1; double motorSpeed = 0.75; boolean isRunning = false; String subsystem = "Drivetrain"; // final = constant, can never be reassigned final int MAX_MOTOR_ID = 20;
FRC connection: The Constants.java file in every robot project is full of final variables — motor IDs, gear ratios, PID values. You'll be writing and reading these all season.
Data Types
Every variable has a type. Types tell Java how much memory to use and what operations make sense. There are primitive types (built-in, lowercase) and non-primitive types (objects, uppercase).
| Type | Category | Example | FRC use |
|---|---|---|---|
| int | Primitive | 5, -3, 1000 | Motor IDs, counter values |
| double | Primitive | 0.75, -1.0 | Motor speeds, sensor readings |
| boolean | Primitive | true / false | Button state, limit switch |
| String | Non-primitive | "Drivetrain" | Logging, Shuffleboard labels |
Type Casting
Converting between types. Small → large (e.g. int → double) happens automatically. Large → small requires an explicit cast and truncates the value — it does not round.
// Implicit — int fits inside double, no data lost int ticks = 500; double pos = ticks; // becomes 500.0 // Explicit — decimal part gets chopped, NOT rounded double reading = 3.87; int truncated = (int) reading; // becomes 3
Operators
% is remainder (modulo). 7 % 3 = 1. Useful for wrapping around array indices.speed += 0.1 is shorthand for speed = speed + 0.1. You'll write this constantly.i++ adds 1. i-- subtracts 1. Used in every for loop.== (compare) with = (assign) — that bug is silent and brutal.Readability & Naming
We have a lot of programmers. Everyone thinks differently. Code that makes sense to you at midnight might be unreadable to someone else the next morning — including future you.
// BAD double x1 = 0.8; int y = 3; boolean f = true; // GOOD — self-documenting double maxShooterSpeed = 0.8; int shooterMotorID = 3; boolean isShooterEnabled = true;
Conventions: Variables & methods → camelCase. Classes → PascalCase. Constants → SCREAMING_SNAKE_CASE. Never use Java keywords (int, class, for) as names.
Fill in the Blanks
motorID = 5;
final MAX_SPEED = 1.0;
double val = 4.9;
int result = ()val;
int i = 0;
; // i is now 1
Knowledge Check
Coding Challenge
Write a snippet that: declares a final double GEAR_RATIO = 8.46, a double motorRPM = 5400.0, calculates wheelRPM = motorRPM / GEAR_RATIO, casts it to an int, and puts a comment above every line.