Variables and Data Types
Data is information that a program stores and uses. In an FRC robot program, data can include a motor's speed, a sensor reading, whether a button is pressed, or a message shown to the driver.
Java has different data types for different kinds of information. For example, int stores a whole number, while boolean stores either true or false.
Data Types
int
int stores whole numbers such as 1, 2, 3, and 67:
int year = 2026;
int price = 123;
int motorSpeed = 6000;
double
double stores decimal numbers such as -0.5, 114.514, and 6.7:
double pi = 3.14;
double currency = 0.71;
boolean
boolean stores one of two values: true or false.
boolean isRobotOn = true;
boolean iLikeCarrots = false;
String
String stores text. In FRC programs, strings are often used for log messages and information displayed on the driver's dashboard. The Hello World program is a simple example of printing a string.
System.out.println("I love programming!");
System.out.println("I was lying."); // println starts a new line after the text
And the output will be:
I love programming!
I was lying.
Basic Math Operators
Operators are symbols that perform calculations or assign values.
| Operator | Explanation | Example |
|---|---|---|
+ | Adds two values | total = 5 + 3; |
- | Subtracts one value from another | remaining = 10 - 4; |
* | Multiplies two values | distance = speed * time; |
/ | Divides one value by another | average = total / count; |
% | Returns the remainder after division | remainder = 10 % 3; |
= | Assigns a value to a variable | motorSpeed = 0.5; |
+= | Adds a value to a variable | score += 2; |
-= | Subtracts a value from a variable | speed -= 0.1; |
++ | Increases a value by one | count++; |
-- | Decreases a value by one | count--; |
Variables vs Constants
A variable is a named place where data is stored. Its value can change while the program is running.
int myAge = 16;
myAge = 17; // I turned 17
int motorSpeed = 0;
motorSpeed = 6000; // Set the motor speed to 6000
Here, motorSpeed starts at 0 and then changes to 6000.
A constant also stores data, but its value cannot change after it is assigned. In Java, constants use the final keyword. Their names are usually written in uppercase with underscores between words.
final double PI = 3.14;
final int CONTROLLER_PORT = 0;
The Java compiler reports an error if you try to change PI or CONTROLLER_PORT because both are declared with final.
Now that you have learned Java's basic data types, you are ready to explore conditions and loops.