Home / Summer / Week 1
Summer · Week 1 of 8

The Basics

Variables, data types, operators, scope, and writing code other people can read.

Variables

A variable is a named container for a value. You give it a type, a name, and a starting value. In FRC you use variables constantly — motor IDs, speeds, sensor readings, on/off flags. Naming them well is half the job.

java
// type name = value;
int     motorID    = 1;
double  motorSpeed = 0.75;
boolean isRunning  = false;
String  subsystem  = "Drivetrain";

// final = constant, can never be reassigned
final int MAX_MOTOR_ID = 20;

FRC connection: The Constants.java file in every robot project is full of final variables — motor IDs, gear ratios, PID values. You'll be writing and reading these all season.

Data Types

Every variable has a type. Types tell Java how much memory to use and what operations make sense. There are primitive types (built-in, lowercase) and non-primitive types (objects, uppercase).

TypeCategoryExampleFRC use
intPrimitive5, -3, 1000Motor IDs, counter values
doublePrimitive0.75, -1.0Motor speeds, sensor readings
booleanPrimitivetrue / falseButton state, limit switch
StringNon-primitive"Drivetrain"Logging, Shuffleboard labels

Type Casting

Converting between types. Small → large (e.g. intdouble) happens automatically. Large → small requires an explicit cast and truncates the value — it does not round.

java
// Implicit — int fits inside double, no data lost
int ticks = 500;
double pos = ticks; // becomes 500.0

// Explicit — decimal part gets chopped, NOT rounded
double reading = 3.87;
int truncated = (int) reading; // becomes 3

Operators

Math
+  −  *  /  %
% is remainder (modulo). 7 % 3 = 1. Useful for wrapping around array indices.
Assignment
+=  −=  *=  /=
speed += 0.1 is shorthand for speed = speed + 0.1. You'll write this constantly.
Increment/Decrement
++  −−
i++ adds 1. i-- subtracts 1. Used in every for loop.
Comparison
==  !=  <  >  <=  >=
Return a boolean. Don't mix up == (compare) with = (assign) — that bug is silent and brutal.

Readability & Naming

We have a lot of programmers. Everyone thinks differently. Code that makes sense to you at midnight might be unreadable to someone else the next morning — including future you.

java — bad vs good
// BAD
double x1 = 0.8;
int y = 3;
boolean f = true;

// GOOD — self-documenting
double maxShooterSpeed  = 0.8;
int    shooterMotorID   = 3;
boolean isShooterEnabled = true;

Conventions: Variables & methods → camelCase. Classes → PascalCase. Constants → SCREAMING_SNAKE_CASE. Never use Java keywords (int, class, for) as names.

Fill in the Blanks

// Declare an int variable for a motor ID of 5
motorID = 5;
// Declare a constant double for max speed
final MAX_SPEED = 1.0;
// Explicitly cast a double to int
double val = 4.9;
int result = ()val;
// Increment i by 1
int i = 0;
; // i is now 1

Knowledge Check

Coding Challenge

Motor Calculator
Apply variables, types, and operators

Write a snippet that: declares a final double GEAR_RATIO = 8.46, a double motorRPM = 5400.0, calculates wheelRPM = motorRPM / GEAR_RATIO, casts it to an int, and puts a comment above every line.