Java is the primary language for FRC robot programming via WPILib, and understanding its core constructs is essential before working with subsystems and commands. Unlike Python, Java is statically typed and requires semicolons at the end of every statement — the compiler will reject code that violates either of these rules. This page covers the fundamental language features you’ll encounter repeatedly in the Spectrum 3847 codebase.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spectrum3847/2026-Spectrum/llms.txt
Use this file to discover all available pages before exploring further.
Variables and data types
Variables and data types
Every variable in Java must be declared with an explicit type before it can be used. The most common types in FRC code are:
Once a variable is declared, its type cannot change:The
| Type | Description | FRC example |
|---|---|---|
int | Whole numbers | Motor port numbers, sensor counts |
double | Decimal numbers | Motor speeds (0.75), distances in meters |
boolean | true or false | Sensor state, button pressed |
String | Text | Subsystem names, telemetry keys |
final keyword prevents reassignment after initialization. Use it for hardware port constants and config values that should never change:Arithmetic and compound operators
Arithmetic and compound operators
Java supports the standard binary arithmetic operators. When both operands are Compound assignment operators are shorthand for updating a variable in place:The
int, division truncates the decimal portion:Use
double when you need fractional results. 5.0 / 2 evaluates to 2.5, while 5 / 2 evaluates to 2.Math class provides additional operations. For example, Math.abs() is commonly used when checking whether a motor output magnitude crosses a threshold:Logic operators and conditionals
Logic operators and conditionals
Comparison operators produce a Logical operators combine multiple boolean expressions:
Conditionals branch execution based on a boolean expression:
boolean result and are used inside if conditions and Trigger definitions:| Operator | Meaning | Example |
|---|---|---|
&& | AND — both must be true | isFuelLoaded && launcherReady |
|| | OR — either can be true | intaking || ejecting |
! | NOT — inverts the value | !sensorTripped |
Strings and string operations
Strings and string operations
String is not a primitive type — it is a class — but it behaves like one for everyday use. Strings are enclosed in double quotes and support several built-in methods:Loops
Loops
Loops repeat a block of code. Java provides three varieties:For loop — use when the number of iterations is known:Enhanced for loop — iterates over every element in an array or collection:While loop — use when the number of iterations is not known in advance:Variable scope inside vs. outside a loop:
In FRC command-based code, traditional
while loops are rarely used for robot control because the command scheduler handles periodic execution. See Applying Java concepts to FRC robot code for details.Arrays and enums
Arrays and enums
Arrays store multiple values of the same type at fixed indices (starting at Enums define a fixed set of named constants — ideal for robot states. The Spectrum 2026 robot uses an enum to represent every possible operating state:Using an enum instead of raw strings or integers makes intent explicit and prevents typos:
0):Classes, objects, and methods
Classes, objects, and methods
A class is a blueprint; an object is an instance of that blueprint. In FRC, every subsystem is a class, and the Methods define what a class can do. The general format is:Access modifiers control visibility:Static vs. non-static:Static members belong to the class itself, not to any instance. Non-static members belong to a specific object:
Robot class instantiates one object of each:public— accessible from anywhereprivate— accessible only within the same class
private and most methods public.Lambda expressions pass behavior as a value — used extensively when building Commands and supplying config values:Command-based programming
See how these Java fundamentals power WPILib’s command-based framework.
Applying Java to FRC
Learn which Java constructs are used most in FRC and how commands replace loops.
