Skip to main content

you better understand Java primitives and references

 

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.

Data Type

Primitive values have a different size and value range.

Bates and Sierra, 2005

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 variable
String color;
}
//Creating another class TestDog which contains the main method
class TestDog{
public static void main(String args[]){
//Creating an object
Dog mydog=new Dog();
mydog.name = “Tommy”;
mydog.color =  “Black”     //initialize object of Dog
System.out.println(mydog.name);//accessing member through reference variable
System.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.

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

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

Bates and Sierra, 2005

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.

Bates and Sierra, 2005

Create new Dog objects and assign them to array elements like this,

Pets[0] = new Dog ();
Pets [1] = new Dog();

Look at the picture.

Bates and Sierra, 2005

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

Popular posts from this blog

How Object Behave?

  How Object Behave? Photo by Ellen Qin on  Unsplash Hello everyone, In This article, you will learn about how the state and how to relate to one another. We know about objects and variables. I give you a small reminder class that describes what an object knows and what an object does. A class is not an object. The object is used to create a class. It tells the JVM how to make an object of that particular type. Each object made from that class can have a value for the instance variable. Bates and Sierra, 2005 if you want to remind read my article first. links are here - A Trip to Objects and Classes , Know your Variables . It will help you. you already know that every object of that type can have a different instance variable. what about the method? Can objects of that type have different method behaviour? Every instance of a class has the same methods, but depending on the value of the instance variables, the methods can behave differently. Look at this example. Do...

The basics you need to know about Java Programming Language

  JAVA- Beginners need to know The basics you need to know about Java Programming Language Hello everyone, you are new to Java, This article is for you. This article great place to start learning JAVA. Let’s start What is the JAVA? Java is a Programming Language and platform-independent. It’s friendly syntax, object-oriented and has strong memory management and portability. The history of Java is very interesting. Read the history of Java by following the Link Click here —  History of Java . Platform : Any hardware or software environment in which a program runs is known as a platform. Java code can run on a variety of platforms, such as Windows, Linux, Sun Solaris, Mac OS, and others. You can understand more about it. Let’s see H ow the way Java Works Step 1 : Create a source code file. (with the .java extension) Source code means to hold on to class definition. class represent a piece of the program. the class has one or more methods. for example, In the student clas...