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:
- A type - what kind of data it will store.
- A name - so you can refer to it later.
- 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;
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!";
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
| Type | What It Stores | Example |
|---|---|---|
int | Whole numbers | 42 |
double | Decimal numbers | 3.14 |
boolean | True/false | true |
String | Text | "Hello" |
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.