Variables and Datatypes
Variables are a core concept in any programming language. Think of them as a container to store a single piece of data. You need to define what type of data you will put into the container and give it a name. Variables should be named to easily identify what they contain.
Declaring a Variable
You can create a variable without assigning a value to it. You would do this is you plan on using it, but do not have a value to assign at creation. The syntax is [type] [name]
which can be seen is examples below.
int team_number;
double pi;
boolean robot_is_on;
String message;
When naming a variable try to stick to the team naming standard. For a typical variable that is using snake_case
which is shown in the example above.
It can be dangerous leaving a variable unassigned at creation. You may know that you can't read it until it has a value, but if other developers are unaware and may read from it before its assigned. This can cause the code to error or crash.
Assigning a Variable
When you define a variable you are assigning it a value to store. The syntax for this is [name] = [value]
which can be seen in examples below.
team_number = 4143;
pi = 3.14;
robot_is_on = true;
message = "I am a string!";
You can actually declare and assign a value to a variable in a single line. This is the preferred process since it will ensure the variable has a default value at the start of the program. This is done with the syntax [type] [name] = [value]
.
int team_number = 4143;
Datatypes
You have seen a few of them already in the examples above, but there are plenty of datatypes in Java. A datatype is the type of data that will be referenced and in this case stored in our variables. Below is a list of a few common types that we use in our robot programs.
Datatype | Description | Example |
int | A non-decimal number |
|
double | A decimal number |
|
boolean | A value that is either true or false |
|
String | A list of characters (a.k.a. text) *note the capitalization in String |
|
Enums
If we wanted to create our own "datatype" with a fixed set of values we can create a enum
. This is a special data type that allows for the definition of a fixed set of named values. We may want to use these to represent a system state or mode.
enum STATE {
IDLE,
SCORE,
HOME,
TRAVELING_UP,
TRAVELING_DOWN
}
A variable can be created to store a type of your custom enum
. You can then reference the members of the enum for assignments and conditions. Think about how a double
datatype can only represent a decimal number and must be compared to decimal numbers. It doesn't make sense to compare a double
to a String
because there is no meaningful comparison between 3.14
and robot
. The same applies for our enum
. You can see an example below of how it might be used.
STATE current_state = STATE.IDLE;
if(current_state == STATE.SCORE){
// Do scoring action!
}
Sure we could use ints
to represent our states where 0 is IDLE
, 1 is SCORE
, 2 is HOME
..., but this makes our code easier to understand and much more readable to a unfamiliar developer.
The above "use ints
to represent our states" comment is actually what the enum is doing under the hood. You can actually use an enum to represent anything you would like. Check out our 2025 robot code where we use it to store elevator targets including all of the height/angle information. This gave us an easy way to organize and reference all of our elevator target throughout the code.
Constants
Variables that are never changed and are used in fundamental calculations / settings are called constants. We can add the final
keyword in front of the datatype to "lock" the variable. The final
keyword ensures that changes cannot be made to the variable accidentally. Constants are also typically named using SCREAMING_SNAKE_CASE
;
final double PI = 3.14159265;
final double WHEEL_BASE = 0.34;
final int SHOOTER_MOTOR_ID = 10;