What’s a variable?
This is a concept that is a basic building block of programming languages and is not specific to Java nor to Object Oriented Programming. If you are familiar with what a variable is feel free to skip this section.
Variables have both 1) a value and 2) an identifier that we can use to access that value, either to read it or to modify it.
Let’s look at some examples in Java:
Example 1
int year = 2017;
Here, we are declaring a variable named year
(the identifier) and we are assigning it an initial value of 2017
.
In Java, all variables need to have a defined type, which indicates the type of data of the value that will be stored. The type precedes the name of the variable, in this case, int
, which is used to store integer numbers. We’ll cover other data types in future sections.
In this example, there are actually 2 separate things happening in the same line:
-
We are defining the
year
variable of typeint
: This is theint year
part of the line. -
And we are also assigning it an initial value: The
= 2017
part of the line.
Example 2 - Assigning a value of a mismatching type
int year = 2017.5;
We are doing the same as in the previous example, but instead of assigning a value of 2017
, we are trying to assign a value of 2017.5
.
This code fails to compile and Java would report an error. When declaring the variable, we used the type int
, that is used to store integer numbers, but we are trying to assign a non-integer value to it.
Java would indicate that it cannot convert from 2017.5 to an integer value.
Example 3 - Modifying the variable’s value
int year = 2017;
year = 2015;
As mentioned before, the value of variables can be modified. In this case, our year
variable started with a value of 2017
and later in our code, we are changing its value to 2015
.
Here, the previous value of the year
variable is lost (unless of course we store it in a separate variable).
Notice that in the second line we don’t specify the type of the variable (int
). This is because, the variable has already been declared in the first line. In the second line we are just making use of a variable that already exists.
Example 4 - Declaring the variable twice
int year = 2017;
int year = 2015;
To clarify what we mean by the last paragraph, assume we had the code above. In this case, we are telling Java that we are trying to declare two separate variables with the same name year
, both of type int
.
This code wouldn’t compile correctly and would report an error. The error would be in line 2, where Java would indicate that there is a duplicate variable called year
.
Example 5 - Declaring and then assigning a value
int year;
year = 2017;
The code above is perfectly valid. We can declare a variable int year
without having to assign it an initial value explicitly.
We’ll cover default values later and explain when Java will assign a default value to variables and when not.
Example 6 - Reading the variable’s value
int year1 = 2017;
int year2 = year1;
In this case we are defining two separate int
variables, one called year1
and one called year2
.
The initial value of year1
is set to 2017
, and the initial value of year2
is set to the value stored in year1
.
In this case, in the 2nd line int year2 = year1
, by using the identifier of year1
Java will read the value of the variable and use that value in the operation, resulting in 2017
being assigned to year2
as well.
Things to take into account
-
This is a very quick look into the concept of variables as we’ll be using it in all the course. There is more to cover in terms of variables, but this should be enough for the first chapters.
-
Java defines different types of variables and the different types of variables have different names. We’ll be introducing them as we go along. You’ll find that their names are used interchangeably in some cases and this can cause confusion. The main combination that is used interchangeably is
variable
andfield
which we’ll cover when we explain classes.