Chapter 1 - Java for Beginners Course

Variables

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:

  1. We are defining the year variable of type int: This is the int year part of the line.

  2. 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

  1. 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.

  2. 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 and field which we’ll cover when we explain classes.

Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations