INTRODUCTION
The main reason we use variables, not only in Java but in any programming language, is to temporarily store values so that we can later use them at various points in our application.
In order to achieve the desired result, we first need to store the values we want and then define the algorithm in which they will be involved. The way to temporarily store these values within our code is through variables.
In today’s free Java lesson, we will focus exclusively on the rules we need to follow in order to correctly define variables within our code.
HOW TO DEFINE A VARIABLE
When we define a variable, we are actually reserving a portion of the computer's memory. When we assign a value to the variable, that value is placed in the memory location that the variable has already reserved. To use the stored value, we simply call the variable's name, and it retrieves the value from memory.
After this quick analysis, we understand that variables generally have three properties:
- They refer to a location in memory.
- They accept a specific type of value.
- And of course, they have a name.
Before we talk about the types of values we can assign to a variable, we need to define the basic rules we must follow in order to give the variable a name.
- The first character in a variable's name must be a letter.
- The characters considered as letters are in the range of 'A'-'Z', 'a'-'z', '_', or '$'.
- After the first character, we can also include numbers ('0'-'9') in the name.
- If the name consists of two or more words, spaces are not allowed between them.
- Other symbols such as '+' or '©' are not allowed.
- We can use as many characters as we want without any limit.
- Java is case sensitive, so we need to be careful with uppercase and lowercase letters.
- The name of the variable cannot be one of the reserved keywords that Java is already using.
Here are some examples of valid variable names in Java:
Welcome, _welcome, $Welcome, Welcome1
While we cannot have names like:
my name, 1Welcome
The list of reserved words is as follows:
With the introduction of the module system in Java 9, ten new reserved keywords were added, which we cannot use as variable names. They are as follows:
open, module, requires,
transitive, exports, opens, to, uses, provides, with.
Note: The name of the variable, meaning the characters that make up the variable's name, is called an identifier. Every time we name a variable, we are essentially defining an identifier. The rules for defining an identifier are the same for naming classes as well as methods.
In order to fully define a variable, besides the identifier (the name of the variable), we must also define the type of data (data type) that it will hold. Java operates as a strongly typed programming language, which means that the values we assign to variables must match the variable's type. The rule for defining a variable is as follows:
data_type variable_name;
The following example defines a variable named num
that will hold integer values:
int num;
PRIMITIVE DATA TYPES
Java consists of two main categories of data types: those for objects and those for non-objects. In this section of free Java lessons, and in a few subsequent ones, we will focus on the category of non-object data types, also known as primitive data types. They are called "primitive" because they are primarily used as local variables and store the actual values themselves. The other type is reference data types, where variables store the memory address where the information is located. This concept will become clearer when we talk about objects later. In total, there are eight different types of primitive data types, as shown in the table below.
Each of the above data types requires a different number of bits to represent a number in the computer's memory. This way, we avoid wasting memory on the device running our Java application. While this may not matter much on laptops or tower computers, it is an important feature for applications created for mobiles, smart TVs, and any other devices where Java runs. Why use 64 bits to represent the number 3 when you can do it with just 8 bits?
In the next free Java lesson, we will start looking at examples of how to define each of these data types and how they are used in the code.
Don't forget to make a small donation so that this blog can grow even more and offer more features in delivering free online lessons.
0 Comments
What do you think about Ground of Code?