Gray Matter
WorkshopJava Basics
WPILib 2027 is still in alpha: these pages change as the APIs settle.
LESSON 08

Java Basics

We will not be teaching you full Java. If you have no Java experience, we recommend a few free lessons below. Even without them, we will cover things as we go.

7 minutes
You’ll need
  • Nothing installed. No project, no robot, no build.
  • A free Codecademy account, which the course below needs.

Learn Java on Codecademy

Codecademy's Learn Java is free and runs in the browser, so there is nothing to install. Four modules cover almost everything the code here uses.

ModuleWhat it gives you
1. Hello WorldBraces, semicolons and comments.
2. Variablesdouble, boolean, and what final does.
3. Object-Oriented JavaClasses, objects, new, constructors, methods. The one that matters most here.
4. Conditionals and Control Flowif, else, and the comparison operators: < > == != && || !.

Stop after Conditionals and Control Flow. We will not use arrays, string methods or loops.

Learn Java on Codecademy: free, in the browser, nothing to install

The six pieces

There are six main pieces to the code you will be writing. The course above covers all of them except the lambda.

PieceIn this workshop
classA kind of thing. class Arm is the drawing, and new Arm() builds one real arm from it.
fieldWhat an object owns for as long as it lives. private final TalonFX motor is one.
constructorSame name as the class, no return type. It runs once, the instant new Arm() runs, and you never call it by name.
methodA named block you can call. Read its return type first: void hands nothing back.
lambdaCode written down and handed over rather than run. () -> setVoltage(3.0) is one, and () -> stopMotor() is another. The () -> is what makes it a parcel instead of a call.
the dota.b() means "on the thing a, call b". What you may type after a dot depends on the type in front of it.

Marking a field private means only code inside Arm.java may touch it, and public is the opposite.

Another word you will see is implements. Writing public class Arm implements Mechanism gives Arm everything Mechanism can do. Mechanism has a method called runRepeatedly(...), for instance. Look through Arm.java and you will not find that method written anywhere. You can call it anyway.

Arm.java, branch mech-2-Commands: the method, the lambda, and the dot
/** Push the arm at 3 volts and keep pushing. Never finishes. */
public Command runSlow() {
return runRepeatedly(() -> setVoltage(3.0)).named("runSlow");
}
WATCH OUT

Writing a lambda runs nothing

Call arm.runSlow() and setVoltage(3.0) does not happen. The method builds a Command and hands it back with the lambda parked inside, and something else runs that command later.

Check your work

Five questions on the lines above. If you miss more than one, go back to the course before the next lesson.

Check yourself

In runRepeatedly(() -> setVoltage(3.0)), when does setVoltage(3.0) run?

Why is it () -> stopMotor() and not stopMotor() inside runRepeatedly(...)?

Arm.java never contains the word runRepeatedly, and runSlow() calls it. Why does that compile?

When does the code inside public Arm() run?

runRepeatedly(...).named("runSlow") compiles, but arm.runSlow().named("lift") does not. Why?

Pick an answer for each.