Head First Java Chapter 03

primitives and references

Sachin Nagendran
6 min readNov 6, 2021

Variables come in two types primitive and reference. We can define Variables as object states (instance variables), local variables (variables declared within a method), arguments (values sent to a method by the calling code), and return types (values sent back to the caller of the method).

Java cares about type. It won’t let you put a floating-point number into an integer variable, unless you acknowledge to the compiler that you know you might lose precision (like, everything after the decimal point).In the real world, we can’t put a Giraffe in a Rabbit variable.

Primitives hold fundamental values including integers, booleans, and floating-point numbers. Object references hold references to objects.

variables must need to have type and name.

Primitive Types

A variable is just a cup. A container. It holds something. primitives come in different sizes, and those sizes have names. When you declare a variable in Java, you must declare it with a specific type. Each primitive variable has a fixed number of bits and has no additional methods.

There are eight primitive data types in Java

primitive type
The sizes for the six numeric primitives in Java

We can’t put a large value into a small cup. If we do that we will lose some. we will get spillage. The compiler tries to help prevent this if it can tell from our code that something’s not going to fit in the container we’re using. For example, we can’t pour an int-full of stuff into a byte-sized container.

int x = 24; 
byte b = x; //won’t work!!

The value of x is 24, and 24 is small enough to fit into a byte. We know that, but all the compiler cares about is that you’re trying to put a big thing into a small thing, and there’s the possibility of spilling.

Back away from keyword

We can name a class, method, or variable according to the following rules.

The name must start with a letter, underscore (_), or a dollar sign ($). You can’t start a name with a number.

After the first character, you can use numbers as well.

It can be anything you like, subject to those two rules, just so long as it isn’t one of Java’s reserved words.

Reserved Words in Java

Reserved words are words that cannot be used as object or variable names in a Java program because they’re already used by the syntax of the Java programming language. If use the reserved keywords we will get errors.

reserved words in java

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects

The main difference between primitive and non-primitive data types are

  • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and are not defined by Java (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while a non-primitive type starts with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size.

references types don’t hold the object itself, but it holds something like a pointer. Or an address. Except, in Java, we don’t know what is inside a reference variable. We do know that whatever it is, it represents one and only one object. And the JVM knows how to use the reference to get to the object

The primitive variable is full of bits representing the actual value of the variable, an object reference variable is full of bits representing a way to get to the object. the dot operator (.) on a reference variable to say, “use the thing before the dot to get me the thing after the dot.

Example — myDog.bark();

steps of object declaration, creation and assignment

1.Declare a reference variable

Dog myDog = new Dog();

Tells the JVM to allocate space for a reference variable, and names that variable myDog. The reference variable is, forever, of type Dog.

2.Create an object

Dog myDog = new Dog();

Tells the JVM to allocate space for a new Dog object on the heap.

3.Link the object and the reference

Dog myDog = new Dog();

Assigns the new Dog to the reference variable myDog.programs the remote control.

Life on the garbage-collectible heap

Declare two Book reference variables. Create two new Book objects. Assign the Book objects to the reference variables. The two Book objects are now living on the heap.

Book b = new Book(); 
Book c = new Book();
References: 2
Objects: 2
Example HeadFirst Java book

Declare a new Book reference variable. Rather than creating a new, third Book object, assign the value of variable c to variable d. Both c and d refer to the same object. The c and d variables hold two different copies of the same value.

Book d = c;References: 3 
Objects: 2
Example HeadFirst Java book

Assign the value of variable b to variable c. The bits inside variable b are copied, and that new copy is stuffed into variable c. Both b and c refer to the same object.

c = b;
References: 3
Objects: 2
Example HeadFirst Java book

Life and death on the heap

Declare two Book reference variables. Create two new Book objects. Assign the Book objects to the reference variables. The two book objects are now living on the heap.

Book b = new Book(); 
Book c = new Book();
Active References: 2
Reachable Objects: 2

Assign the value of variable c to variable b. The bits inside variable c are copied, and that new copy is stuffed into variable b. Both variables hold identical values. Both b and c refer to the same object. Object 1 is abandoned and eligible for Garbage Collection (GC).

b = c;
Active References: 2
Reachable Objects: 1
Abandoned Objects: 1
Example HeadFirst Java book

The first object that b referenced, Object 1, has no more references. It’s unreachable.

Assign the value null to variable c. This makes c a null reference, meaning it doesn’t refer to anything. But it’s still a reference variable, and another Book object can still be assigned to it. Object 2 still has an active reference (b), and as long as it does, the object is not eligible for GC.

c = null;
Active References: 1
null References: 1
Reachable Objects: 1
Abandoned Objects: 1
Example HeadFirst Java book

Quick points

A reference variable value is the bits representing a way to get to an object on the heap

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.

--

--

Sachin Nagendran

Associate Software Engineer @ Virtusa | Flutter | Dart | Java