Skip to main content

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.

OperatorExplanationExample
+Adds two valuestotal = 5 + 3;
-Subtracts one value from anotherremaining = 10 - 4;
*Multiplies two valuesdistance = speed * time;
/Divides one value by anotheraverage = total / count;
%Returns the remainder after divisionremainder = 10 % 3;
=Assigns a value to a variablemotorSpeed = 0.5;
+=Adds a value to a variablescore += 2;
-=Subtracts a value from a variablespeed -= 0.1;
++Increases a value by onecount++;
--Decreases a value by onecount--;

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.