Lesson 2

Java Variables: Storing Data on Our Journey to the Stars

Topic Overview and Goal Setting

Welcome back! As we continue our Java Journey to the Stars, we're gearing up to better understand Java Variables, our fundamental companions. Like celestial objects, variables illuminate our code, enriching it with data and information.

In simplistic terms, a variable in coding resembles a box — a designated spot in memory where a value can be stored. This lesson aims to break down the concept of Java variables, exploring their definition, the conventions for naming and value assignments, and the idea of constant variables.

What are Java Variables?

Visualize Java variables as tiny boxes filled with data. The short example below demonstrates how a variable is created in Java:

1int numberOfStars; // We declare a variable, akin to opting for a box 2numberOfStars = 88; // We then fill it with a value 3System.out.println(numberOfStars); // Finally, we verify its contents. It outputs: 88

Here, int is the variable's data type (integer number), numberOfStars is the variable's name, and 88 is its value. We will cover data types in the next lesson, so don't worry if the int part is not clear for now.

However, there is a shorter way of doing the same - you can create and assign the variable at the same time. Here is an example:

1int numberOfStars = 88; // creating and assigning the variable with a value 2System.out.println(numberOfStars);
Java Naming Conventions

Just as labeling a box effectively, choosing a name for a Java variable necessitates certain rules and conventions. These ensure that our code remains free of errors and is easy for others to understand.

The variable name should start with a lowercase letter and adhere to the CamelCase convention: if the variable name contains just a single word, all letters should be lowercase; if the variable name contains multiple words, however, the first one should be lowercase, and each of the next ones should start with a capital letter. For example, age, weight, myAge, firstDayOfWeek.

Certain characters, such as digits and special characters, are not permissible at the beginning of variable names.

1// Correct variable naming 2int myAge = 20; 3int myWeight = 72; 4int district9Population = 10000; 5 6// Incorrect variable naming (commented intentionally) 7// int 0zero = 0; 8// int ?questionMark = 1;
Assignment Operations in Java

Assignment in Java involves allocating or changing a value to a variable using the = operator. This process resembles placing an item in a box.

1int constellations = 88; // We design a box, label it and deposit a value inside 2System.out.println(constellations); // We inspect its contents. It outputs: 88 3 4constellations = 77; // We change the value of the variable 5System.out.println(constellations); // We inspect the updated contents. It outputs: 77
Java Constants

While the previous section explains how to change the value of the variable, Java also provides a way to define constants, i.e., variables that cannot change their value once assigned. We use the final keyword to declare a constant. The naming convention for constants is to use uppercase letters with words separated by underscore _.

It is a good practice to declare a value as final if you know that it's not going to change for various reasons such as readability, safety (avoid unintentional modifications), and sometimes for performance.

1final int DAYS_IN_WEEK = 7; // We solidify a constant, akin to etching a fact in stone 2System.out.println(DAYS_IN_WEEK); // We scrutinize our engraved fact. It outputs: 7 3 4// DAYS_IN_WEEK = 6; // This will not compile and run

Here, DAYS_IN_WEEK serves as a constant, prohibiting alterations once assigned. The value for this variable cannot be changed after it's assigned.

Lesson Recap and Next Steps

Congratulations! You've now gained insights into the basics of variables and constants in Java. In our forthcoming lessons, we'll put these concepts to the test with hands-on exercises. Practice is vital to converting knowledge into skills, so let's dive into the tasks and maintain the momentum on our Java Journey to the Stars!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.