Know Your Variables
you better understand Java primitives and references
Hello everyone, Today you will learn about what is primitives and references.
Variables
The variable is container holds the value while the Java program is executed. you have used variables in two places. The first one is the instance variable other one is the local variable. Later we will use variables as an argument and as a return type. you have seen the variable declare as a primitive type.
Declaring the Variable
Let’s see how to declare the variable. Variable must have the type and the name.

Don’t mess up. you will understand it while you reading the article till the end.
Let’s see what is the Primitive Datatypes
The different sizes and values that can be stored in the variable are defined by data types. Data types have two types.
primitive data types — These include boolean, char, byte, short, int, long, float and double.
non-primitive data types — These include Classes, Interfaces, and Arrays.

Primitive values have a different size and value range.

Let’s see How primitive data types declaration and initialization in Java.
int a, b, c; // Declares three int, a, b, and c.
int a = 10, b = 20; // declare a int type and assign a value a,b
byte B = 22; // declare a byte type and assign value B.
boolean isCrazy; // declare a boolean name isCrazy.
isCrazy = true; // value true the previously declared isCrazy.
double si = 2.1415; // declares a double type and assigns a value.
char a = 'b'; // char variable is initialized with value 'a'Look at the Java code Program.
public class Simple{
public static void main(String[] args){
int x=50;
int y=10;
int z=a+b;
System.out.println(z);
}
}out put — 60
Object References
Now you know about declaring a primitive variable and how to assign it a value. Let’s see about Object Reference
There are three steps declaration, creation and Assignment.
1. Declare the reference variable
Dog mydog
The JVM to allocate space for a reference variable and name that variable myDog. The reference variable of type Dog.
2. create an object
new Dog();
Java Virtual Machine(JVM) allocates space for new dog objects on the heap.
3. Link the object and the reference
Dog mydog = new Dog();
Assign the new dog to the reference variable my dog.
As you can see objects gets the memory in the heap area. The reference variable refers to the stack area.

Look at this Code,
class Dog{String name; // instance variableString color;}//Creating another class TestDog which contains the main methodclass TestDog{public static void main(String args[]){//Creating an objectDog mydog=new Dog();mydog.name = “Tommy”;mydog.color = “Black” //initialize object of DogSystem.out.println(mydog.name);//accessing member through reference variableSystem.out.println(mydog.color);}}
mydog.name();
you can use the dot operator(.) to a reference variable which means using the object referenced by the mydog to the name() method.
Life on Garbage Collectible on the heap
Look at the following example, you can understand well.
Book b = new Book();
Book c = new Book();Create two new book objects and assign the book objects to the reference variable.

Then you can see two book Objects living on the heap. Then we can say like that,
Reference: 2
Objects: 2
Book d= c;Declare a new book reference variable d, then the third book object assigns the value of variable c to d.

Both c and d hold on to the same object. We can say like that,
Reference 3
Object 2
c=b;Both c and b refer to the same object.

I hope you can understand it.
Let’s look at the other example.
Book b = new Book();
Book c = new Book();Create two new book objects and assign the book objects to the reference variable.

Then you can see two book Objects living on the heap. Then we can say like that,
Active Reference: 2
Reachable Objects: 2
b=c;Assign the value of variable c to variable b.

then you can both b and c refer to the same object but one object is abandoned, then it’s eligible for garbage collection. (GC). Then we can say like that,
Active Reference: 2
Reachable Objects: 1
Abandoned Objects 1
c= null;Assign the value null to variable c.

Object 2 still has an active reference (b), and it is not eligible for GC as long as it does. Then we can say like that,
Active References: 1
null References: 1
Reachable Objects: 1
Abandoned Object: 1
Arrays are Objects too
Let’s see how to declare the array variable,
Int[]nums;Create a new int array assign in the array variable. Then new array length of 7
Nums = int[7];Look at the picture

We give each element in the array an int value. Elements in an int array are just int variable
Nums[0]= 2;Nums[1]= 12;Nums[2]= 15;Nums[3]= 10;Nums[4]= 5;Nums[5]= 18;Nums[6]= 28;Let’s make an array of Dogs
First, declare a Dog array variable.
Dog[] pets;Secondly, you create a new Dog array with length and assign it.
Pets = new Dog[7];Look at the picture.

Create new Dog objects and assign them to array elements like this,
Pets[0] = new Dog ();Pets [1] = new Dog();Look at the picture.

Conclusion
I hope, in this article, you can get knowledge about Java Variable and References. I will cover Java in upcoming articles. Stay on with me.
Reference
Bates, B. and Sierra, K., 2005. Head First Java, 2nd Edition. [S.l.]: O’Reilly Media, Inc.
Comments
Post a Comment