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.
- 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.
| Module | What it gives you |
|---|---|
| 1. Hello World | Braces, semicolons and comments. |
| 2. Variables | double, boolean, and what final does. |
| 3. Object-Oriented Java | Classes, objects, new, constructors, methods. The one that matters most here. |
| 4. Conditionals and Control Flow | if, 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 installThe six pieces
There are six main pieces to the code you will be writing. The course above covers all of them except the lambda.
| Piece | In this workshop |
|---|---|
| class | A kind of thing. class Arm is the drawing, and new Arm() builds one real arm from it. |
| field | What an object owns for as long as it lives. private final TalonFX motor is one. |
| constructor | Same name as the class, no return type. It runs once, the instant new Arm() runs, and you never call it by name. |
| method | A named block you can call. Read its return type first: void hands nothing back. |
| lambda | Code 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 dot | a.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.
/** Push the arm at 3 volts and keep pushing. Never finishes. */public Command runSlow() { return runRepeatedly(() -> setVoltage(3.0)).named("runSlow");}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?