Sitemap
Karnan Sooriyakumar

Karnan Sooriyakumar

Variables

3 min readMar 19, 2022

A variable is just a cup. A container. It holds something. It has a size, and a Type.

Java is a type safety language and before executing the code compiler check the type safety.

In java variable come in two flavors firestone primitive and object reference. Primitives’ types are byte, short, int, long, float, double, char, Boolean. Reference variables hold references to objects.

When we declare a variable that variable should have name and suitable type.

Primitive

Consider variable as like tea cups. You can’t put a large value into a small cup and all primitive variables have their own capacity or size.

Press enter or click to view image in full size
Primitive type Variable max sizes

You can assign a value to a variable in one of several ways including:

• type a literal/value after the equals sign (x=12. isGood = true, etc.)

• assign the value of one variable to another (x =y)

• use an expression combining the two (x = y + 43)

When you put a variable name, you need to consider some criteria’s:

It must start with a letter, underscore ( _ ), or dollar sign ($). You can’t start a name with a number. After the first character, you can use numbers as well. Just don’t start It with a number. You can not use java reserved keywords.

Press enter or click to view image in full size
Java reserved keywords

non-primitive variables or Object Reference

Although a 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.

It doesn’t hold the object Itself, but It holds something like a pointer. Or an address. Except., in Java we don’t really 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.

Garbage Collection

In java, garbage means unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.

Get Karnan Sooriyakumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Arrays

Arrays are always objects, whether they’re declared to hold primitives or object references.

But you can have an array object that’s declared to hold primitive values. In other words, the array object can have elements which are primitives. But the array itself is never a primitive, regardless of what the array holds, the array itself is always an object.

An array is like a tray of cups.

int[] nums;nums = new int[7] ;
Press enter or click to view image in full size

Declare an array of Dog (Dog is a class)

Dog[] pets;pets = new Dog [7];

we have an array of Dog reference (pets), but that array does not contain actual dog objects only having the 7 Dog object references.

when we assign values inside the arrays like this, 7 Dog objects created in heap memory and only save 7 references inside the Dog type array object.

pets[0] = new Dog();pets[l] = new Dog();--pets[6] = new Dog();
Press enter or click to view image in full size

References

[1] Sierra, K., & Bates, B. (2005). Head First Java, 2nd Edition (2nd ed.). O’Reilly Media.

--

--