Skip to content

Programming Basics: Variables, Data Types, and Operators

learnfrc.com
learnfrc.comAuthor
Veer Bajaj
Veer BajajMaintainer

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.

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.

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 — your robot only ever stops or slams to full speed.

  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).

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.

  • 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.

This lesson was adapted from learnfrc.com.