Data Types Lesson

What kind of data are we storing?

In Java, every variable must have a data type. This tells the computer what kind of information to expect—a whole number, text, a decimal, or a true/false value—and how much memory to set aside for it.

Primitive Data Types

Primitive types are the most basic data types available in Java. They are the fundamental building blocks for storing simple, single values. Java is a "statically-typed" language, which means you must explicitly declare the type of every variable. There are eight primitive types in total.

int: The Workhorse for Whole Numbers

The int data type is used for storing integers (whole numbers without decimals). It's the most common numeric type you'll use for things like counters, scores, and IDs.

  • Size: 4 bytes
  • Range: -2,147,483,648 to 2,147,483,647
// FRC Example: Storing a team number
int teamNumber = 2910;

// FRC Example: Counting game pieces scored
int piecesScored = 5;

double: For Precision and Decimals

The double data type is used for floating-point numbers (numbers with decimal points). It offers a high degree of precision, making it the default choice for any fractional calculation, like motor speeds or sensor readings.

  • Size: 8 bytes
  • FRC Use: Essential for motor outputs, joystick inputs, and PID calculations.
// FRC Example: Setting a motor speed (from -1.0 to 1.0)
double motorSpeed = 0.75;

// FRC Example: Storing a precise angle from a gyroscope
double armAngle = 45.8; 

boolean: The Logic Gatekeeper

The boolean data type is the simplest of all. It can only hold one of two values: true or false. Booleans are the foundation of all decision-making (if-statements) and loops in your code.

  • Size: Logically 1 bit
  • FRC Use: Reading limit switches, checking conditions, and controlling state machines.
// FRC Example: Checking if a limit switch is pressed
boolean isArmAtBottom = true;

// FRC Example: A flag for our autonomous routine
boolean isRedAlliance = false;

char: A Single Character

The char data type is used to store a single character. Character literals are always enclosed in single quotes ('). It's less common in FRC, but useful to know.

  • Size: 2 bytes
  • Values: Any single letter, number, or symbol.
// Example: Storing a letter grade
char grade = 'A';

// Example: Storing a special symbol
char currencySymbol = '$';

Other Numeric Types: For Memory Optimization

Java provides other numeric types for situations where you need to save memory or handle numbers outside the range of an int. These are less common in typical FRC code but are important to recognize.

  • byte: Tiny integer (-128 to 127). Uses only 1 byte.
  • short: Small integer (-32,768 to 32,767). Uses 2 bytes.
  • long: Huge integer, for when an int isn't big enough. Must end with an 'L'. Uses 8 bytes.
  • float: A less precise decimal number. Must end with an 'f'. Uses 4 bytes.
byte smallValue = 100;
long veryLargeNumber = 9000000000L;
float lessPreciseDecimal = 0.5f;

Non-Primitive (Reference) Data Types

Unlike primitive types which store a single, simple value, non-primitive or "reference" types store a reference (or memory address) to a more complex object. They are created from classes and give you access to powerful built-in behaviors (methods).

The Almighty String

The String is arguably the most important non-primitive type. It's a sequence of characters, used for any kind of text. String literals are always enclosed in double quotes (").

// FRC Example: Naming an autonomous routine
String autoName = "Two Piece Center";

// Strings come with powerful built-in methods
String message = "Hello, FRC!";
int length = message.length(); // length is 11
String upper = message.toUpperCase(); // upper is "HELLO, FRC!"

Arrays and Other Objects

Arrays and any other object you create from a class (like a Spark motor controller or an Encoder) are also reference types. They hold collections of data or represent complex hardware components.

// An array that holds multiple integers
int[] motorPorts = {1, 2, 3, 4};

// An object representing a hardware device (more on this later!)
Spark intakeMotor = new Spark(5);

Test Your Knowledge

Question: You need to store the speed of a shooter wheel, which will be a value like 0.85. Which data type is the most appropriate for this variable?