Java: Variables

This lesson primarily deals with the concept of variables, including how to label, name, declare, and initialize them.

What are the variables?

In simple terms, variables are names allotted to the data stored in programs or data needed to perform operations. For example, to obtain and store a user’s age, the data can be named userAge. To declare this variable named userAge, the following statement must be typed:

int userAge;

The statement used to declare a variable follows a simple format. First, the variable’s data type appears, followed by the variable name, with a space in between these components. As is obvious from the phrasing, the data type indicates the type of data that the variable will store (such as a numeric value or a few words). In the example stated, the data type used is int, used to denote integers.

The variable’s name is userAge. Once the variable userAge is declared, some part of the computer’s memory space will store it. The name userAge can be used to both access and alter its data. 

Primitive Data Types in Java

In Java language, there are eight predefined data types, also known as primitive data types. 

Out of these, 4 data types store integers, which are numbers devoid of fractional parts. The four data types are:

byte

This data type is ideal in situations with constraints in terms of storage or there is a need to save space. However, byte data type only stores numbers ranging from -128 to 127, and the storage space (also known as the width or the size of a data type) used up is 1 byte. 

To acquire the ages of the users, the byte data type would be the ideal one to use, as it is likely that the values of the ages entered will not be over 127.

short

When using an integer ranging from -32768 to 32767, the short data type is preferred. Its width is 2 bytes.

int

The int data type is commonly used since it has the most useful range of -231 (-2147483648) to 231-1 (2147483647). It uses 4 bytes of storage space. 

long

The long data type is not very useful as its range of numbers is impractical for regular usage, which is -263 to 263-1. So, unless there is a need to store a very large number, such as the number of people present on the Earth right now, this data type should not be used. Its width is 8 bytes and the suffix “L” must be added to specify a long value at the end of a number. The next lesson will elaborate more on suffixes.  

Apart from the data types used to store integers, many data types are used to store floating-point numbers, including numbers containing fractional parts. These data types are:

float

The float data type uses 4 bytes of storage space, and the range of numbers it accepts is from -3.40282347 x 1038 to positive +3.40282347 x 1038. The float data type ensures the precision of about seven digits. When entering a number with ten digits, such as 1.234567890, the number stored is a rounded-off version, comprising only seven digits: 1.234568.   

double

If the input number may range from negative 1.79769313486231570 x 10308 to positive 1.79769313486231570 x 10308, the double data type is appropriate. The storage space used by this data type is in bytes and gives a precision of 15 digits.

Anytime a floating-point number is specified in Java, the system, by default, considers it a double and not a float input. To ensure that Java considers it a floating number, integrate the letter “F” right after the number. If there are no space-related concerns in the system, it is always good to use a double instead of a float to ensure better precision. 

Apart from the data types mentioned above, Java has two more under primitive data types, which are: 

char

char refers to a character, and it stores singular Unicode characters, including ‘T,’ ‘%,’ ‘#’ and ‘s,’ among others. Its width is 2 bytes.

boolean

The data type of boolean is special and can hold only true and false as its values. This data type typically finds its use in control flow statements, which the subsequent articles discuss.    

Naming a Variable

When naming variables in Java, it is important to keep in mind the various rules. Firstly, in Java, only letters, numbers, underscore (_), or/and the dollar sign ($) are available to use in a variable name. However, the variable name cannot begin with a number. Variables can be named as $Age, Age_, _Age, Age1, and Age1$ among several other combinations, but cannot be named starting with a digit such as; 1Age.   

However, typically, people begin their variable name with a letter instead of the permitted special characters. Moreover, the dollar sign is usually never used in a variable name, even though it is allowed.  

It is always a good practice to keep the variable names short and full of meaning to ensure that even a layperson can understand its purpose. Therefore, it is always better to use variable names such as Name, Age, and Number, as opposed to na, ag, and un.

Apart from this, there are a set of ‘reserved words’ that cannot be used as variable names, since in Java, they already hold pre-determined meanings. Some of the reserved words are System, if, while, etc. The lesson addresses these topics at a later point.  

Typically, to name variables in Java, people use camel casing, using multiple words in the variable name, with each word starting with a capital letter, barring the first word, such as ageOfUser.   

In the entire lesson, this format is followed.  

Lastly, since Java is case sensitive, the variables are too. Therefore, ageOfUser is different from ageofuser

Initializing a Variable

After choosing a variable name and declaring it with a data type, it is time to assign it an initial value. This process is known as initializing the variable. The value of the variable can be changed at any point.  

There are two ways to initialize a variable. Initialize it directly during the variable’s declaration or use another statement to do the same. 

The statements mentioned below serve as examples regarding how variables can be initialized during the declaration statement.    

byte Age = 36;
short salaryofEmployee = 32000;
int numberOfTables = 800;
long inhabitantsOnEarth = 32569390022878L; 
float dailyWage = 90.7F;
double yearlyWage = 7369.7;
char level = 'C';
boolean promote = false;
byte stage = 3, customerSatisfaction = 4;

As discussed, it is necessary to specify a long value by integrating the letter “L” as a suffix at the end of a number. Therefore, the program followed this rule in the fourth line. In case this step is overlooked, the compiler will return an error message stating that the number is very long.  

Furthermore, while initializing the variable dailyWage, the letter “F” was integrated as the suffix. Java tends to consider every floating-point number declared, to be double, if “F” does not exist at the end of the inputted number.   

Lastly, initializing a char data type requires the inputted character to be enclosed in single-quotes.  

The last example showcases how, using just one declaration statement, variables can be initialized and declared, as long as they are of the same data type. Each of the variables is separated using a comma. 

The examples above demonstrate how the same statement can perform both declaration and initialization of a variable. The same variables can also be declared and initialized in two different statements, such as this: 

int weight;	//this is the declaration statement 
weight = 120; //initialization comes next  

Leave a Reply

%d bloggers like this: