Skip to content
2 min read
In progress

Programming Basics: Variables, Data Types, and Operators

Meet the three building blocks behind every program: places to store data, the kinds of data you can store, and the operators that let you work with it.

Lesson
1 / 51
Reward
+10XP

Sign in to track progress, earn XP, and save lessons.

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 like 3, or a counter.
  • double — a decimal number, e.g. a motor output 0.75 or a distance in meters.
  • boolean — only true or false: "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 — Java won't even let you assign a double to an int without a cast, and forcing it truncates every fractional output to 0, so the motor just sits there.

Operators

  1. Arithmetic: + - * / % (% is remainder). Trap: int / int truncates, so 5 / 2 is 2. Make one side a double (5.0 / 2) to get 2.5 — easy to hit when converting encoder ticks to distance.
  2. Comparison: ==, !=, <, >, <=, >= — each produces a boolean.
  3. Logical: && (and), || (or), ! (not), e.g. isEnabled && !isStopped.
  4. Assignment shortcuts: count += 1; is the same as count = 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.

Spot an error or something out of date?Log in to suggest an edit

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.

Lesson quiz

Required

Answer all 3 questions correctly to complete this lesson.

01.Which Java type should hold an FRC motor output value like 0.75, and why?

02.What is the result of the Java expression 5 / 2, and why?

03.Which operator checks whether two values are equal in Java?

Answer every question to submit.

All 51 lessons in Programming, Controls & Sensors