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:

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:

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.

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.

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.

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal