Head First Java Chapter 03 — Know Your Variables

Nimesh Mendis
3 min readJun 8, 2022

--

Hello Readers,

This article is to highlight the main points include in the chapter.

Declaring a Variable

  • Java is dealing with type. As a result, you must declare the type of your variable in order for this type safety to work.
  • Variables are two types:
    1. Primitive Variables
    2. Object Reference Variables

When you declaring a variable you need to consider these 2 rules:
1. Variable must have a type
2. Variable must have a name

Primitive Variables

Primitive types are the most basic data types available within the Java language. There are 8: boolean, byte, char, short, int, long, float and double. These types serve as the building blocks of data manipulation in Java (Source: https://wikibooks.org).

  • You can put a large value such as long into a little value such as int without losing any information.
  • However, a little value can be assigned to a large variable.

Primitive Naming Convention.

Here is the rules for naming a variables.
1. It must start with a letter, underscore(_), or dollar sign ($). You can’t start a name with a number.
2. After the first character, you can use numbers as well, just don’t start it with a number.
3. It cannot be from the Java reserved words.

Object Reference Variables

  • There is actually no such thing as an object variable.
  • There’s only an object reference variable.
  • An Object reference variable holds bits that represent a way to access an object.
  • It doesn’t hold the object itself.
  • Object reference are like Remote Control for actual object.
  • You cannot do arithmetic on a reference variable.

The 3 steps of object declaration, creation and assignment

  1. Declare a reference variable — Dog myDog = new Dog();
  2. Create an Object — Dog myDog = new Dog();
  3. Link the Object and the Reference — Dog myDog = new Dog();

Array

  • Declare an int array variable, An array variable is a remote control to an array object.

int[] nums

  • Create a new int array with a length of 3.

nums = new int[3]

  • Give each element in the array an int value.

nums[0] = 6;

nums[1] = 5;

nums[2] = 4;

  • Arrays are always objects, whether they’re declared to hold primitives or object reference.
  • Once you’ve declared an array, you can’t put anything in it except things that are of the declared type.

BULLET POINTS

  • Variables come in two flavors: primitive and reference.
  • Variables must always be declared with a name and a type.
  • A primitive variable value is the bits representing the value (5, ‘a’, true, 3.1416, etc.).
  • A reference variable value is the bits representing a way to get to an object on the heap.
  • A reference variable is like a remote control. Using the dot operator (.) on a reference variable is like pressing a button on the remote control to access a method or instance variable.
  • A reference variable has a value of null when it is not referencing any object.
  • An array is always an object, even if the array is declared to hold primitives. There is no such thing as a primitive array, only an array that holds primitives.

--

--