Reference data type in Java

79
views
Java Reference data type.png

In this tutorial, you’ll learn Reference data type in Java and different data types in Java Language.

Before Starting with Reference data type in Java lets first understand what Data Type is.

In simple words, The term datatype refers to the type of data that can be stored in a variable.

There are two data types available in Java:

Table of Contents

Reference/Object Data Types

In Java, a reference data type is a variable that can contain the reference or an address of the dynamically created object. These type of data type are not predefined like a primitive data type. The reference data types are arraysclasses, and interfaces that are made and handle according to a programmer in a java program  which can hold the three kinds of values as:

array type
// Points to an array instance

class type
// Points to an object or a class instance

interface type
// Points to an object and a method, which is implemented to the corresponding interface

class type:

As you know that Java is an object-oriented programming language where an object is a  variable, associated with methods that are described by a class. The name of a class is treated as a type of a Java program so that you can declare a variable of an object type, and a method which can be called using that object- type variable.

Whenever a variable is created, a reference to an object is also created using the name of a class for its type i.e. that variable can contain either null or a reference to an object of that class. It is not allowed to contain any other kinds of values. Such type is called reference types in Java. The object becomes an instance when the memory is allocated to that object using the new keyword. In addition, array types are reference types because these are treated as objects in Java. For example:

class Fruit {
     fColor(){....}
     fSize(){....}
};
Fruit mango;
Fruit banana;
...

In the given example the Fruit is a class that has the reference variables as mango & banana through which we can call the behaviours associated with that class as mango.fColor(); within the main method of the superclass.

Array Type:

An array is a special kind of object that contains values called elements. The Java array enables the user to store the values of the same type in contiguous memory allocations. The elements in an array are identified by an integer index which initially starts from and ends with one less than a number of elements available in the array. All elements of an array must contain the same type of value i.e. if an array is a type of integer then all the elements must be of integer type.  It is a reference data type because of the class named as Array implicitly extendsjava.lang.Object. The syntax of declaring  the array is shown as:

DataType [] variable1, variable2, .......variableN;
DataType [] variable = new DataType [ArraySize];

DataType [] variable = {item 1, item 2,...item n};
int [] a = new int [10];
String [] b = {"reference","data", "type"};
Example:

In the first statement, an array variable “a” is declared of integer data type that holds the memory spaces according to the size of int. The index of the array starts from a[0] and ends with a[9]. Thus, the integer value can be assigned to each or a particular index position of the array.

In the second statement,  the array “b” is declared of string data type that has enough memory spaces to directly holds the three string values.  Thus each value is assigned for each index position of the array.

Interface Type:

Java provides another kind of reference data type or a mechanism to support multiple inheritance features called an interface. The name of an interface can be used to specify the type of a reference. A value is not allowed to be assign to a variable declared using an interface type until the object implements the specified interface.

When a class declaration implements an interface, that class inherits all of the variables and methods declared in that interface. So the implementations for all of the methods declared in the interface must be provided by that class. For example, Java provides an interface called ActionListener whose method named actionPerformed() is used to handle a different kind of event. Java also provides a class called Thread that implements Runnable interface.
Thus the following assignment can be allowed:

Runnable r;
r = new Thread();

Ask your questions and clarify your/others doubts on Java Reference Data Type by commenting. Documentation

PREVIOUS
JAVA PRIMITIVE DATA TYPES
NEXT
JAVA VARIABLES