Unit 2: Java Basics
Variables are one of the most fundamental concepts in programming. Think of them as labeled containers that allow your programs to store, manage, and manipulate information on the fly.
At its core, a variable is a named storage location in your computer's memory that holds a piece of information. This information, or "value," can be used and changed as your program runs. Let's break down the analogy.
This represents the variable itselfβa container in memory reserved for storing data.
This is the variable's name. It's a unique identifier you choose so you know what's inside the box.
This is the variable's valueβthe actual piece of information you store inside the box.
You can change what's inside the box (the value), but the label on the box (the name) stays the same once it's created.
Before you can use a variable, you must "declare" it. This tells the computer you want to create a new storage container. In Java, a statically-typed language, you must also specify what *type* of data the container will hold.
dataType variableName = value; // Example declarations in Java: int robotScore = 150; double motorSpeed = 0.75; boolean isRedAlliance = true;
Choosing clear, consistent names is crucial for writing readable code. While there are strict rules (like no spaces), there are also style conventions that make your code professional. The most common styles are:
The first word is lowercase, and the first letter of each subsequent word is capitalized. This is the standard convention for variables and methods in Java.
int playerScore; double motorSpeed; String teamName;
The first letter of every word is capitalized. In Java, this convention is used almost exclusively for naming classes.
public class RobotController { ... }
public class SwerveModule { ... }All words are lowercase and separated by underscores. While you won't see this often in Java, it's the standard convention in other languages like Python and is sometimes used for constants in Java.
final int MAX_SPEED = 3;
Scope defines the region of your code where a variable can be accessed. A variable declared inside one method isn't accessible from another. Understanding scope is key to organizing your code and avoiding errors.
Declared inside a method or code block (like a loop). They only exist and can only be used within that block.
public void calculateScore() {
int bonusPoints = 50; // 'bonusPoints' is a local variable
// It can only be used inside this method.
int totalScore = baseScore + bonusPoints;
}Declared inside a class but outside any method. Each object (instance) of the class gets its own personal copy of this variable.
public class Robot {
public String robotName; // An instance variable
public double weight; // Another instance variable
public void displayStats() {
// Both variables are accessible here.
System.out.println("Name: " + robotName);
}
}Declared with the `static` keyword. There is only one copy of this variable, shared by all objects of the class. It belongs to the class itself.
public class Team {
public static int teamNumber = 2910; // A static variable
public void printTeamNumber() {
// All objects of the Team class share this one number.
System.out.println(Team.teamNumber);
}
}Question: According to Java conventions, which of the following is the best name for a variable that stores the current speed of an arm motor?