498
Control Lab FRC Programming Curriculum
Fundamentals · L03 of 8

Types and operators

Prereqs: fundamentals-02
Objectives 0 / 3

Hook

You write code to run a motor at half speed:

int speed = 1 / 2;
motor.set(speed);

The motor doesn’t move. 1 / 2 in Java is 0, not 0.5. Integer division throws away the decimal. The robot does exactly what you wrote — not what you meant.

Core concept

Key Concept

Every value in Java has a type — a declaration of what kind of data it is and what operations make sense on it. Using the wrong type produces wrong answers silently.

The four types you’ll use constantly

TypeWhat it holdsExampleRobot use
intWhole numbers42, -7, 0Loop counters, game-piece counts
doubleDecimal numbers0.5, -1.0, 90.3Motor power, angles, speeds
booleanTrue or falsetrue, falseSensor flags, mode switches
StringText"hello", "arm"Log messages, names
int gamePieces = 3;
double motorPower = -0.75;
boolean hasPiece = true;
String subsystemName = "intake";

Arithmetic operators

These work on int and double:

OperatorMeaningExampleResult
+Add3 + 25
-Subtract90.0 - 45.045.0
*Multiply0.5 * 21.0
/Divide10.0 / 42.5
%Remainder10 % 31
⚠ Heads up

Integer division truncates. When both operands are int, Java discards the decimal: 7 / 23, not 3.5. Use 7.0 / 2 or (double) 7 / 2 when you need the fraction.

Comparison operators

These always produce a boolean:

OperatorMeaningExampleResult
==Equalangle == 90true or false
!=Not equalcount != 0true or false
<Less thanpower < 0.5true or false
>Greater thanangle > 88true or false
<=Less or equalspeed <= 1.0true or false
>=Greater or equalangle >= 0true or false

Logical operators

These combine boolean values:

OperatorMeaningExampleResult
&&AND (both true)hasPiece && armUptrue only if both
||OR (either true)atMin || atMaxtrue if either
!NOT (flip it)!hasPieceopposite of hasPiece

Robot example:

boolean readyToShoot = hasPiece && armAtTarget && driverPressed;
boolean limitHit = atTopLimit || atBottomLimit;

Interactive demo

Trace through this code. Notice how integer division and comparisons interact:

Code Tracer
1int pieces = 7;
2int slots = 2;
3int perSlot = pieces / slots;
4double exact = pieces / (double) slots;
5boolean full = pieces >= 5;
6boolean needMore = !full;
State
pieces7
slots2
perSlot
exact
full
needMore

Step through to see values update.

Press Start to begin stepping through the code.
Initial state

Try it yourself

⚡ Try it yourself

Before stepping through, predict each value:

double power = 3.0 / 4;
int count = 10 % 3;
boolean inRange = (power > 0.5) && (count < 5);
Code Tracer
1double power = 3.0 / 4;
2int count = 10 % 3;
3boolean inRange = (power > 0.5) && (count < 5);
State
power
count
inRange

Step through to see values update.

Press Start to begin stepping through the code.
Initial state
⚡ Check your understanding

What does int result = 9 / 2 evaluate to?

Key takeaways

  • Use double for anything that can have a decimal (motor power, angles, distances).
  • Use int for counts and loop indices where fractions don’t make sense.
  • int / int always truncates — add .0 to one operand when you need precision.
  • Comparison operators return boolean, which is what if statements require.

Common confusions

“Why does 1 / 2 equal 0?” Java doesn’t know you want a fraction — you gave it two integers. It treats / as “how many whole times does 2 go into 1?” The answer is 0.

“When do I use = vs ==?” Single = assigns a value (angle = 90). Double == compares two values (angle == 90). Using = inside an if is a common bug Java will usually catch.

Challenge

⚡ Try it yourself

An encoder reads 1500 ticks. The maximum is 4000 ticks. Compute the position as a percentage (0.0 to 1.0) and print it. Also check: is the position past the halfway point? Print true or false.

Watch out for integer division — both values start as int.

Code EditorJavaCtrl+Enter to run
Stuck? Show hint

Use (double) ticks / maxTicks to avoid integer division. pastHalf is just position > 0.5.

What’s next

In Lesson 04, we’ll use these comparisons inside if/else chains to make the robot choose different behavior depending on sensor values.