dev only

Variables & Data Types

Primitive Types: a Small, Fixed List

Java has exactly 8 primitive types — the only types built directly into the language. A primitive variable holds its value directly, with nothing more underneath it.

The ones you'll use constantly in FRC code:

  • int — a whole number, like 5 or -12. CAN IDs, ports, loop counters.
  • long — a bigger whole number, for values too large for int (like a timestamp). Written with an L: 100L.
  • double — a decimal number, like 0.85. Almost anything measured — speed, distance, voltage.
  • float — a smaller, less precise decimal number. Written with an f: 1.5f. Most FRC code (including WPILib itself) prefers double instead.
  • boolean — only ever true or false.

The rest exist too, but you'll rarely reach for them directly:

  • byte, short — smaller whole numbers than int. Rarely needed.
  • char — one single character, like 'A'. You won't use this much on its own, but it's worth knowing: this is what String is actually built from underneath.

That's the full list — nothing else in Java is a primitive. Everything else is built out of these 8, most obviously String, which is really a sequence of chars glued together. Later, when you write your own classes (starting in Ch.7), those will be built by bundling primitives together — a robot subsystem might combine a double for speed, a boolean for "is it ready," and more, all wrapped into one object. Keep this in the back of your mind going forward: every new type you meet is either one of this fixed list, or something built on top of it.

Declaring and Assigning

To create a variable, write its type, then its name, then a semicolon:

int motorSpeed;

motorSpeed now exists, but it doesn't hold a value yet. Give it one with =:

motorSpeed = 5;

Read that line as: motorSpeed gets assigned the value 5. Not "motorSpeed equals 5" — = in code isn't the same as = in math class. It's an instruction: take what's on the right, and store it in the box on the left.

Usually you do both steps at once:

int motorSpeed = 5;

Read this the same way: motorSpeed, of type int, is assigned the value 5.

A variable declared inside a method body, like everything here, is called a local variable — it only exists inside that method. (Java has other kinds of variables too — you'll meet those once classes and methods are on the table, starting in Ch.7.)

Naming Variables (camelCase)

A name must start with a letter and can't contain spaces. Java's style — you'll see this in every piece of code from here on — is camelCase: start lowercase, then capitalize the first letter of each new word.

double maxSpeed = 0.85;   // not max_speed, not MaxSpeed
boolean isReady = false;

Pick a name that says what the value is. speed tells you something; x doesn't.

Worked Example

int motorSpeed = 5;         // a whole number — no decimal needed here
double maxOutput = 0.85;    // a percentage like 85% — needs a decimal
boolean isReady = false;    // only ever true or false
String robotName = "Titan"; // text, built out of primitives under the hood

System.out.println("Robot " + robotName + " is set to speed " + motorSpeed);

Common Pitfalls

  • Never put a variable's name inside quotes. System.out.println("motorSpeed") prints the literal word motorSpeed, not the number stored in it. Only actual words/sentences go inside ""; variables get attached with +.
  • Case matters. motorSpeed and motorspeed are two different variables to Java. This doesn't cause an error you'll notice right away — it just quietly breaks something.
  • Match the type to the value. If a value has a decimal point, it needs double, not int — otherwise Java throws away everything after the decimal point.

Key Takeaways

  • Java has exactly 8 primitive types. int, long, double, float, boolean are the common ones in FRC code; byte, short, char exist but are rarely used directly.
  • Everything else (String, and every class you'll write) is built out of these 8 — String is a sequence of chars.
  • A variable is a named box that holds a value, and that value can change.
  • = means "gets assigned," not "equals."
  • A variable declared inside a method is a local variable — it only exists in that method. There are other kinds too, covered starting in Ch.7.
  • Variable names use camelCase and should describe what the value is.

Derived from csawesome-2026: unit-1-using-objects-and-methods/1.02-variables-and-data-types.md (CSA 1.2)