Unit 2: Java Basics
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 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.
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.
// FRC Example: Storing a team number int teamNumber = 2910; // FRC Example: Counting game pieces scored int piecesScored = 5;
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.
// 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;
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.
// FRC Example: Checking if a limit switch is pressed boolean isArmAtBottom = true; // FRC Example: A flag for our autonomous routine boolean isRedAlliance = false;
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.
// Example: Storing a letter grade char grade = 'A'; // Example: Storing a special symbol char currencySymbol = '$';
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 smallValue = 100; long veryLargeNumber = 9000000000L; float lessPreciseDecimal = 0.5f;
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 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 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);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?