Java can be divided into two categories # Primitive Data Types and # Reference Data Types
Reference Data Types
As we discussed that the Java programming language are divided into two categories :
- Primitive Data Types
- Reference Data Types
Lets have a discussion about Reference Data Types in brief
In Java a reference data type is a variable that can contain the reference or an address of dynamically created object. These type of data type are not predefined like primitive data type. The reference data types are arrays, classes and interfaces that are made and handle according to a programmer in a java program which can hold the three kind of values as:
array type // Points to an array instance class type interface type |
class type:
As you know that Java is an object-oriented programming language where an object is a variable, associated with methods that is described by a class. The name of a class is treated as a type in 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 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; |
In the given example the Fruit is a class
that has the reference variables as mango & banana through which we
can call
the behaviors associated with that class as mango.fColor(); within
the main method of the super class.
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 0 and ends with one less than 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 the class named as Array implicitly extends java.lang.Object. The syntax of declaring the array is shown as:
DataType [] variable1,
variable2, .......variableN;
DataType [] variable = new DataType [ArraySize]; |
For example:
int [] a = new int [10]; String [] b = {"reference","data", "type"}; |
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 for each or a particular index
position of the array.
In the second statement, the array "b" is declared of
string data type that has the enough memory spaces to directly holds the three
string values. Thus each value is assigned for each index position
of the array.
For more details about the arrays, click to the following link
http://www.roseindia.net/java/master-java/introduction-to-java-arrays.shtml
Interface Type:
Java provides an another kind of reference data type or a mechanism to support multiple inheritance feature 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 the 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(); |