No phone? Tell a mentor and we'll sign you in.
The parts that trip up every rookie, and how to beat them
This is real robot code from an intake (simplified). Click to reveal it.
Right now it probably looks like nonsense. That's OK.
By the end of today, you'll be able to read every line.
int total = 5 + 3;
10110000 00000101 00000100 00000011
.java and .class filesEx01_Hello.javajavacthe Java compilerEx01_Hello.classJVM = Java Virtual Machine: a program that acts like a computer that understands bytecode. The command java Ex01_Hello starts it.
bytecode
JVM for Intel/AMD
JVM for Apple chips
JVM for its ARM chip
Each computer has its own JVM that translates bytecode for its CPU. It even speeds up as it runs, by compiling the busiest parts on the fly.
Ex01_Hello.javajavac Ex01_Hello.javaEx01_Hello.classjava Ex01_HelloHello, World!;int x = "hi";No .class file. Nothing runs.
The program crashes or misbehaves.
C:\bootcampbootcamp.C:\bootcamp. If it asks, click Yes, I trust the authors.
public class Ex01_Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
PS C:\bootcamp> means "PowerShell, in the bootcamp folder, waiting for you." You type after the > and press Enter.PS C:\bootcamp> javac Ex01_Hello.java ↑ javac printed nothing: no news is good news PS C:\bootcamp> dir Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/30/2026 6:02 PM 425 Ex01_Hello.class -a---- 10/30/2026 6:01 PM 124 Ex01_Hello.java PS C:\bootcamp> java Ex01_Hello Hello, World! PS C:\bootcamp> type Ex01_Hello.class Êþº¾ A ... java/lang/Object ... ()V ↑ gibberish to us, perfect for the JVM
C:\bootcamp folder.main.
public class Ex01_Hello {
%%
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
main. Don't see them? Wait for VS Code to finish loading Java (bottom bar), then try again.
public class Ex01_Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class Ex01_Hello {
...
}
File Ex01_Hello.java, class Ex01_Hello. Exactly the same, capitals included.
public class ⟦Ex01_Hello⟧ {
...
}
ex01_hello.java:1: error: class Ex01_Hello is public,
should be declared in a file named Ex01_Hello.javavoid main() { IO.println("Hi"); }. Robot code still uses full classes, so we learn the full form.
public class Robot {
public void teleop() {
if (buttonPressed) {
intake.run();
} else {
intake.stop();
}
}
}
public class Robot {
public void teleop() {
if (buttonPressed) {
intake.run();
} else {
intake.stop();
}
}
}
public class Robot {
public void teleop() {
if (buttonPressed) {
intake.run();
} else {
intake.stop();
}
}
}
int speed = 5;
motor.setThrottle(0.5);
System.out.println("Go!");
public void stop() ⟦;⟧ { // method header
if (x > 5) ⟦;⟧ { // if condition
while (pathClear()) ⟦;⟧ { // loop condition
if (x > 5); { stop(); } compiles, and stop() always runs. The ; ends the if right there, with nothing inside it.
System.out.println("ok"); ✓
⟦system⟧.out.println("no"); ✗
To Java, System and system are different words.
String name = "Ava"; ✓ ⟦string⟧ name = "Ava"; ✗
But int, double, boolean are lowercase.
"Hello" ✓ ⟦“Hello”⟧ ✗
Curly "smart quotes" sneak in when you copy from Docs, Word or a website.
// One-line comment: everything after // is ignored /* A block comment can span many lines */ motor.setThrottle(0.3); // arm hits frame above 0.4
package frc.robot.subsystems; and it must match.org.wpilib.public class Bug1 {
public static void
main(String[] a) {
int x = 5⟦ ⟧
System.out.println(x);
}
}int x = 5public class Bug2 {
public static void
main(String[] a) {
System.out.println("Hi");
⟦}⟧
public class Robot {
public static void
main(String[] a) {
System.out.println("Go");
}
}public class Bug4 {
public static void
main(String[] a) {
⟦system⟧.out.println(1);
}
}public class Bug5 {
public static void
main(String[] a) {
System.out.println(⟦“Hi”⟧);
}
}Robot.java:14: error: ';' expected motor.setThrottle(0.5) ^ 1 error
Back in 10 minutes. Next: data and types.
double speed;speed = 0.5;double speed = 0.5;motor.setThrottle(speed);int x = 5; x = x + 1;
Whole numbers. No decimals. Up to about 2.1 billion.
long for whole numbers past 2.1 billion. Ends in 3_000_000_000LNumbers with decimals. Our default.
float is half the size and less precise. Ends in 0.75fYes or no. Only two values.
One character, in single quotes.
Text, in double quotes. Capital S.
int count = 0.5; won't compile: a decimal doesn't fit in a whole-number box.@@int motorId = 3; @@double speed = 0.75; @@boolean hasGamePiece = false; @@String mode = "auto"; @@final int kIntakeMotorId = 9;
Just true or false. Basically one switch.
Biggest value: 2,147,483,647. Remember Gangnam Style from last session? Need more? long uses 64 bits.
Decimals with about 15 to 16 digits of accuracy. Big range, but not perfect: 0.1 + 0.2 is 0.30000000000000004. float uses 32 bits and keeps only about 7 digits.
double leftMotorSpeed = 0.5; // camelCase boolean isAtTarget = false; // reads as a question final double kMaxSpeed = 0.8; // constant: k...
double ⟦x⟧ = 0.5; // what is x? int ⟦2ndMotor⟧ = 4; // can't start with a digit double ⟦left speed⟧ = 0.5; // no spaces int ⟦class⟧ = 3; // reserved word
add, subtract
multiply
divide (careful!)
remainder: 7 % 3 is 1
2 + 3 * 4 is 14. Use parentheses to be clear: (2 + 3) * 4 is 20.count % 2 == 0 asks "is count even?" Great for blinking lights.++ and --x = x + 1;The long way you already know: "x gets x plus one."
x += 1;Same thing, shorter. Works with any amount: speed += 0.1;
x++;Shortest: add exactly one. Say it "x plus plus."
x--;The opposite: subtract one. Say it "x minus minus."
int x = 5; x = x + 1; // x is 6 x += 1; // x is 7 x++; // x is 8 x--; // x is 7
i++ (next section), and counting down uses i--.You may also see ++x. On its own line, it does exactly the same thing.motor.setThrottle(1 / 2); compiles fine and sets the motor to 0. Write 0.5 or 1.0 / 2."Team " + 245
Text + number = text
"2" + 2
Not 4! "2" is text.
2 + 2 + "2"
Left to right: 2 + 2 is 4, then "4" + "2"
System.out.println("Speed: " + speed); prints a readable message while you debug.if (count ⟦=⟧ 3) // ✗ stores 3 if (count == 3) // ✓ asks "is it 3?"
boolean ready = enabled && !hasGamePiece;
7 / 2"2" + 22 + 2 + "2"10 % 35 == 5.0!(3 > 2)Then run Ex03_Predict.java to check every answer on your laptop.
Back in 10 minutes. Next: making decisions and repeating.
if / else · click the switch
boolean hasGamePiece = true;
if (hasGamePiece) {
shooter.fire();
} else {
intake.run();
}
else if · drag the slider
double speed;
if (distance > 5.0) {
speed = 0.9; // far: go fast
} else if (distance > 2.0) {
speed = 0.5; // medium
} else {
speed = 0.2; // close: creep in
}
if (mode ⟦=⟧ 2) { ... }Use == to compare.
if (x > 5)⟦;⟧ { stop(); }stop() always runs.
if (ready)
intake.run();
shooter.fire(); // always runs!Without braces, only the first line belongs to the if. Always use braces.
if (mode ⟦==⟧ "auto") // ✗ unreliable
if (mode.equals("auto")) // ✓Strings use .equals().
while loop stepper · press Stepint laps = 0;while (laps < 3) { System.out.println("Lap " + laps); laps++;}System.out.println("Done!");
laps++ and the loop never ends.for loop stepper · press Stepfor (int i = 0; i < 3; i++) { System.out.println("Lap " + i);}System.out.println("Done!");
for (int i = 0; i < 3; i++)
0 1 2
3 laps: 0, 1, 2
for (int i = 0; i <= 3; i++)
0 1 2 3
4 laps: 0, 1, 2, 3
for (int i = 0; i < 3; i++). Start at 0, use <, and the number is the lap count.
public void periodic() { // called every 20 ms
// ✗ waits here, nothing else runs:
while (!intake.hasGamePiece()) { }
// ✓ check once per loop instead:
if (intake.hasGamePiece()) { intake.stop(); }
}
10 9 ... 3 Get ready! 2 Get ready! 1 Get ready! Match start!
leftMotor.setThrottle(0); rightMotor.setThrottle(0); intake.stop(); // ...and the same 3 lines // in 6 other places
void stopEverything() {
leftMotor.setThrottle(0);
rightMotor.setThrottle(0);
intake.stop();
}
stopEverything(); // use anywhereSystem.out.println("Hi") and motor.setThrottle(0.5) are method calls. Someone else wrote those recipes.
double average(double a, double b) {
double total = a + b;
return total / 2;
}
double average(double a, double b) { ... } // definition
double avg = average(2.0, 4.0); // call
void teleop() {
if (buttonPressed) {
double speed = 0.8; // born here
motor.setThrottle(speed);
} // gone here
System.out.println(⟦speed⟧); // ✗
}
error: cannot find symbol
symbol: variable speedvoid drive(double speed) void stop() void runIntake()
Do something. Names are verbs.
boolean hasGamePiece() boolean isAtTarget() double getAngle()
Answer something. Booleans read like yes/no questions.
if (intake.hasGamePiece()) { shooter.fire(); }
static boolean isFast(double speed) {
// TODO
return false;
}
Click a layer to turn it on or off.
Same file you saw at the very start of today.
A brace problem
A value in the wrong box
Sneaky division
= vs ==
The robot loop trap
A scope or return mistake
Open Ex06_DebugIntake.java. Fix the compile errors first, then run it and fix what's still wrong.
Data (has): a motor, a beam-break sensor, a speed
Methods (can): run(), stop(), hasGamePiece()
Every subsystem on the robot is a class: Intake, Shooter, Drivetrain, Arm.
Next time: how to build your own, and how they work together.
javac checks and compiles; the JVM runs it.
Braces are boxes. Every { needs its }.
The type decides what fits. Watch 1 / 2.
if / else and loops, but never wait in robot code.
Methods are named recipes.
int x = 7 / 2; x is...
Build your own Intake. Practice with the exercise pack before then.
| →SpacePgDnEnter | Next step or slide (also: click the right half of the screen, or a clicker) |
| ←PgUpBackspace | Back (also: click the left half, or right-click) |
| Home End | First slide / last slide |
| 12… then Enter | Jump to a slide number |
| T | Contents: jump to any slide (or the ☰ button, top-left) |
| N | Show or hide speaker notes |
| F | Fullscreen on / off |
| ? | This help |
| Esc | Close notes, contents, help or a photo |