Skip to content

Variables and Data Types#

The building blocks of every Java program

Overview#

Every piece of information in a Java program is stored in a variable. Before you can use a variable, you must declare it — give it a name and a data type that defines what kind of value it holds.

See table of contents for a breakdown of this section.


Primitive Data Types#

Java has several built-in data types. The four you will use most in FRC programming are:

Type Stores Example
int Whole numbers int motorPort = 1;
double Decimal numbers double speed = 0.75;
boolean True or false boolean isRunning = false;
String Text String subsystemName = "Drivetrain";

Note

String starts with a capital letter because text behaves differently from numbers in Java — don't worry about why yet, just remember to capitalize it. You will learn more in Java Classes.

Java is case sensitive

Int and int are different. Boolean and boolean are different. Always use lowercase for primitive type names.


Declaring and Assigning Variables#

Declaration creates the variable. Assignment gives it a value. You can do both at once or separately:

Declaring and assigning variables
        // Declare only — value is unset
        int motorPort;

        // Assign a value
        motorPort = 1;

        // Assign a new value later
        motorPort = 2;

FRC Example

FRC variable declarations
        double driveSpeed = 0.5;          // half speed
        boolean isShooterRunning = false;
        int leftMotorPort = 1;

Constants and final#

If a variable should never change after it is first set, declare it with final. This prevents accidental reassignment and makes your intent clear.

Local final variable
        final int LEFT_MOTOR_PORT = 1;

In FRC code you will often see public static final together:

Class constant (public static final)
    public static final int LEFT_LEADER_ID = 1;
  • public — any class can read this value
  • static — shared across all uses of the class; accessible without creating an object first
  • final — the value cannot be changed after assignment

Naming convention

Java constants are written in ALL_CAPS_WITH_UNDERSCORES. Regular variables use lowerCamelCase.

Naming conventions
    public static final int MAX_SPEED = 100;   // constant
        double currentSpeed = 0.0;  // variable

Scope#

Where you declare a variable determines where it can be used — this is called scope.

  • Field (class-level variable) — declared outside any method, available to all methods in the class. Fields are covered in depth in Java Classes.
  • Local variable — declared inside a method, only exists while that method is running
Drivetrain.java
public class Drivetrain extends SubsystemBase {

    private double topSpeed = 1.0;  // field — available in all methods

    public void setSpeed(double speed) {
        double adjusted = speed * topSpeed;  // local — only exists in this method
        motor.set(adjusted);
    }
}

Tip

Declare hardware objects (motors, sensors) as fields so every method in the class can reach them. Use local variables for temporary calculations inside a single method.


Operators#

Arithmetic#

Operator Meaning Example
+ Add score + 1
- Subtract distance - offset
* Multiply speed * 0.5
/ Divide ticks / 2048.0
% Remainder (leftover after division) 7 % 21

Comparison (result is always boolean)#

Operator Meaning
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal

Logical (combine boolean values)#

Operator Meaning Example
&& AND — both must be true isRunning && !isStopped
\|\| OR — at least one must be true buttonA \|\| buttonB
! NOT — flips true to false and vice versa !isFinished

Comments#

Comments are ignored by the compiler and are used to explain code to other programmers (including your future self).

Code comments
    // Single line comment

    /*
       Multi-line comment —
       spans multiple lines
    */

    /**
     * Doc comment — appears when you hover over this item in VSCode.
     * Supports HTML formatting.
     */

Naming Conventions#

Consistent naming makes code readable across a team.

What Convention Example
Variables and methods lowerCamelCase driveSpeed, setMotorOutput
Classes UpperCamelCase Drivetrain, ShooterSubsystem
Constants ALL_CAPS_UNDERSCORES MAX_SPEED, LEFT_LEADER_ID
Packages all lowercase frc.robot.subsystems

Knowledge Check#

Quiz results are saved to your browser's local storage and will persist between sessions.

#

Which data type stores whole numbers without a decimal point?

#

What naming convention should Java constants follow?

#

A variable declared inside a method is called a:

#

Which arithmetic operator gives the remainder after division?

#

Which is the correct way to declare a motor port constant that any class can read and that can never change?

Quiz Progress

0 / 0 questions answered (0%)

0 correct