Types and operators
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
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
| Type | What it holds | Example | Robot use |
|---|---|---|---|
int | Whole numbers | 42, -7, 0 | Loop counters, game-piece counts |
double | Decimal numbers | 0.5, -1.0, 90.3 | Motor power, angles, speeds |
boolean | True or false | true, false | Sensor flags, mode switches |
String | Text | "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:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 3 + 2 | 5 |
- | Subtract | 90.0 - 45.0 | 45.0 |
* | Multiply | 0.5 * 2 | 1.0 |
/ | Divide | 10.0 / 4 | 2.5 |
% | Remainder | 10 % 3 | 1 |
Integer division truncates. When both operands are int, Java discards the decimal:
7 / 2 → 3, not 3.5. Use 7.0 / 2 or (double) 7 / 2 when you need the fraction.
Comparison operators
These always produce a boolean:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal | angle == 90 | true or false |
!= | Not equal | count != 0 | true or false |
< | Less than | power < 0.5 | true or false |
> | Greater than | angle > 88 | true or false |
<= | Less or equal | speed <= 1.0 | true or false |
>= | Greater or equal | angle >= 0 | true or false |
Logical operators
These combine boolean values:
| Operator | Meaning | Example | Result |
|---|---|---|---|
&& | AND (both true) | hasPiece && armUp | true only if both |
|| | OR (either true) | atMin || atMax | true if either |
! | NOT (flip it) | !hasPiece | opposite 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:
int pieces = 7;int slots = 2;int perSlot = pieces / slots;double exact = pieces / (double) slots;boolean full = pieces >= 5;boolean needMore = !full;Step through to see values update.
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);double power = 3.0 / 4;int count = 10 % 3;boolean inRange = (power > 0.5) && (count < 5);Step through to see values update.
What does int result = 9 / 2 evaluate to?
Key takeaways
- Use
doublefor anything that can have a decimal (motor power, angles, distances). - Use
intfor counts and loop indices where fractions don’t make sense. int / intalways truncates — add.0to one operand when you need precision.- Comparison operators return
boolean, which is whatifstatements 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
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.
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.