Summary
In this chapter, we learned that a variable is simply a memory location with a name and a value. To utilize variables, we have to know how to declare and access them.
To declare a variable, we specify the variable’s name and its type – for example, int countOfTitles=5;
. This line of code declares an int
variable named countOfTitles
with a value of 5
. Naming them properly using camel case, is a great aid in making your code more readable and maintainable. To access the variable, we just specify the variable’s name – for example, System.out.println(countOfTitles);
.
As Java is a strongly typed language, we have to specify a variables' type when we declare it. Java provides eight in-built primitive data types for our use. They are easily recognizable due to their lowercase letters. In the preceding line of code, int
is the primitive data type for the countOfTitles
variable. We saw the sizes in bytes of the primitive types, which determines...