Skip to main content

Variables & Data Types

Every FTC program you write will need to remember things. In Java, we store and work with this information using variables.

Think of variables as labeled containers in your code. Each one can hold a certain type of data, and the type determines what you can do with it.


What is a Variable?

A variable is a named piece of memory your program can read from or write to. You give it three main things:

  1. A type - what kind of data it will store.
  2. A name - so you can refer to it later.
  3. A value - the actual piece of information it's holding.

Example:

int score = 0; // An integer variable named score, starting at 0

The Most Common Data Types

int - Whole Numbers

Stores integers (whole numbers) like 1, 42, -15, or 0.

int xDistance = 3;
int yDistance = -5;
tip

Use int for counting things, scores, or any whole number that fits within roughly -2 billion to +2 billion.

double - Decimal Numbers

Stores decimal numbers like 3.14, -0.5, or 2.0.

double xOffset = -2.54;
double yOffset = 3.35;

boolean - True or False

Stores only two values: true or false.

boolean INIT = true;
boolean hasReachedTarget = false;

String - Text

Stores text like words, sentences, or any sequence of characters.

String message = "This is a string!";
tip

Notice String starts with a capital letter. It's slightly different from the other types, but don't worry about that for now.


Declaring vs. Initializing

You can declare a variable (create it) and initialize it (give it a value) separately:

// Declare first
double xOffset;

// Initialize later
xOffset = 2.5;

Or do both at once:

int xDistance = 9;

Quick Reference

TypeWhat It StoresExample
intWhole numbers42
doubleDecimal numbers3.14
booleanTrue/falsetrue
StringText"Hello"
Remember

These are the four types you'll most likely be working with at the beginning. As you get more comfortable with programming, you'll learn about other types, but for now this is basically all you'll need.