the video version
The same lesson as a narrated video. Watch it on YouTube
Robot code has to remember things (a sensor reading, a motor output, whether a button is pressed) and do math or comparisons with them. Variables, data types, and operators are how you do both. Everything else in WPILib is built on top of these.
Variables: Named Boxes for Data#
A variable is a named box that holds a value. In Java you say what kind of value it holds when you create it:
int motorPort = 3;
double speed = 0.75;
boolean isEnabled = true;
The pattern is always type name = value;. Creating the box is declaring; putting a value in is assigning. After that, reuse the name without the type:
speed = 0.5;
Name variables for what they hold — armAngle, not x. Java convention is camelCase.
Data Types: What Kind of Value?#
Java checks every variable's type before the program runs, so a typo like assigning text to a number won't compile instead of crashing mid-match. The four types you'll use constantly in FRC:
int— a whole number, e.g. a CAN ID or PWM port like3, or a counter.double— a decimal number, e.g. a motor output0.75or a distance in meters.boolean— onlytrueorfalse: "is the limit switch pressed?"String— text like"intake". It's an object, not a primitive, which is why it's capitalized.
byte, short, long, float, and char exist too, but you'll rarely reach for them as a beginner.
This matters in FRC: WPILib motor outputs are doubles from -1.0 (full reverse) to 1.0 (full forward). Store one in an int and you throw away the decimal — your robot only ever stops or slams to full speed.
Operators#
- Arithmetic:
+ - * / %(%is remainder). Trap:int / inttruncates, so5 / 2is2. Make one side a double (5.0 / 2) to get2.5— easy to hit when converting encoder ticks to distance. - Comparison:
==,!=,<,>,<=,>=— each produces aboolean. - Logical:
&&(and),||(or),!(not), e.g.isEnabled && !isStopped. - Assignment shortcuts:
count += 1;is the same ascount = count + 1;.
The classic beginner bug: = assigns, == compares. if (ready = true) is wrong; write if (ready).
Putting It Together#
double batteryVoltage = 12.4;
boolean lowBattery = batteryVoltage < 11.5; // false here
double outputSpeed = lowBattery ? 0.3 : 0.75; // ternary: compact if/else
With just these three ideas you can already describe most of what a robot tracks every cycle.
the part worth keeping
Key takeaways
- A variable is a named, typed box: `type name = value;` — declare once, reassign freely.
- Java is statically typed; the FRC workhorses are `int`, `double`, `boolean`, and `String`.
- Motor outputs are `double` values from -1.0 to 1.0, so never store them as `int`.
- `int / int` truncates (5/2 == 2); use a `double` operand for decimal results.
- `=` assigns a value; `==` compares — mixing them up is a top beginner bug.
Programming, Controls & SensorsProgramming Foundations: A Java Primerlesson 1 of 4
Keep going
Take the quiz+10 XP with an accountMore in Programming Foundations: A Java Primer
verifiedchecked against primary sources on 9 Sept 2026
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.oracle.comOracle Java Tutorials: Primitive Data Types
- dev.javadev.java: Using Operators in Your Programs
- docs.oracle.comOracle Java Tutorials: Summary of Variables
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 4 min readFRC Drivetrain Types: Tank, Swerve, and MecanumA beginner-friendly guide to FRC drivetrains: how tank, swerve, and mecanum drives work, their tradeoffs, and how to pick one for your robot./blogread it
- 7 min readFRC Programming Languages: Java vs C++ vs PythonAn honest, beginner-friendly comparison of the three WPILib-supported FRC languages — Java, C++, and Python (RobotPy) — with vendor support and how to choose./blogread it
- 20 min readBuilding an FRC Scouting App: Data Model, TBA/Statbotics APIs, and PicklistsBuild an FRC scouting app: match and pit scouting, a simple data model, pulling schedules and EPA from the TBA and Statbotics APIs, and building a picklist./blogread it
answer sheet
Lesson quiz
All 3 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 3 answered
01Which Java type should hold an FRC motor output value like 0.75, and why?
02What is the result of the Java expression 5 / 2, and why?
03Which operator checks whether two values are equal in Java?
Answer every question to submit.
All 51 lessons in Programming, Controls & Sensorsopenclose
01 / prerequisites
02 / foundations-tools-and-first-program
03 / robot-program-and-command-based
04 / motors-and-control
05 / autonomous-trajectories-simulation
06 / sensing-fundamentals
07 / encoders
08 / gyros-imus-orientation
09 / closed-loop-control
10 / vision-pose-estimation
11 / worked-examples-mini-projects
- Not read yet:Mini-Project: A Closed-Loop Elevator with Motion Magic
- Not read yet:Mini-Project: A Velocity-Controlled Shooter on REVLib
- Not read yet:Mini-Project: A Teleop Swerve Drive Subsystem
- Not read yet:Mini-Project: An Autonomous Routine with PathPlanner
- Not read yet:Mini-Project: Vision-Aligned Scoring with Limelight
12 / common-mistakes-troubleshooting
13 / advanced-techniques-case-studies
- Not read yet:State-Space Control and Kalman Filtering
- Not read yet:Log Replay Architecture with AdvantageKit
- Not read yet:Advanced Pose Estimation: Multi-Tag Fusion and Standard Deviations
- Not read yet:Robot Coordination, Alerts, and Operator Feedback
- Not read yet:Case Study: Hardening Software Before an Event