Reference Data Types in Java
In Java, there exist two types of data types that are Primitive data types and Reference data types, which is also referred to as Non-Primitive. The primitive data types are predefined, whereas the Reference data types are dynamically created objects.
The Reference data type creates references to the objects in the memory and does not hold the value of the objects. In the Reference data type, the object's address is stored, which makes its properties and functions accessible.
An example of a Reference type can be:
Bird bird = new Bird(“Owl”);
When a Reference data type is created, the following points hold.
- The variables can only hold the value's address or reference and do not store the value itself.
- The default value is null.
- The reference variable refers to any object of the declared data type or any other compatible data type.
Reference Data Types
The Reference data types in Java are Array, class, Interface, String, Enums and Annotations.
Sr.No | Reference Type | Detail |
Array | Arrays can hold a fixed number of elements of the same data type. | |
Class | The class is a template for defining the objects. | |
Interface | It contains methods or functions which the class must implement. | |
String | It depicts the representation of one or more characters in a sequence. | |
Enum | It is a special form of class that provides the type safety. Every element present in it is an object of that enum. | |
Annotations | It presents a way to link the program's elements with the meta-data. |
Example
Please go through the example below, where we have demonstrated using various reference data types in Java in Java.
FileName: ReferenceTypeExample.java
public class ReferenceTypeExample {
// Creates an Enum
public enum Days{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
// Main method
public static void main(String[] args) {
// Creates an object of the type object
Object o = new Object();
// Creates an object of the type String
String str = "Learning Java";
// Creates an array of type integers
int[] arr = {10,20,30,40,50};
// Display the values
System.out.println(o.toString());
System.out.println(str.toUpperCase());
for(Integer i: arr) {
System.out.print(i + " ");
}
// For readability
System.out.println();
for (Enum e: Days.values()) {
System.out.print(e + " ");
}
}
}
Output:
java.lang.Object@34c45dca
LEARNING JAVA
10 20 30 40 50
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY
Comparing the Reference Types
The Reference types in Java can be compared, and two approaches exist to compare them.
1. Using the == operator
The == is an operator that compares the objects' addresses or references. It returns true if the object reference or address is the same. Note that it does not check for the content of the object.
FileName: CompareReferenceType.java
//Java program to compare reference type
//Using == operator
public class CompareReferenceType {
//Main method
public static void main(String[] args) {
String str1 = "JAVA";
String str2 = "JAVA";
String str3 = new String("JAVA");
// compare using == (equal) operator
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}
}
Output:
true
false
2. Using the String.equals() method.
The equals () is a method in the String class that compares the object's content. It returns true if the content of the object is the same. The main difference between the == operator and equals() method is that the == operator checks for the location, and the equals() method checks for the content.
FileName: CompareReferenceType1.java
//Java program to compare reference type
//Using equals() method
public class CompareReferenceType1 {
//Main method
public static void main(String[] args) {
String str1 = "JAVA";
String str2 = "JAVA";
String str3 = new String("JAVA");
// compare using == (equals) operator
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
}
}
Output:
true
true
Conversion between the Primitive and Reference Type
In Java, wrapper classes facilitate the conversion between the Primitive and Reference types. It provides the mechanism of autoboxing and unboxing, which enables the conversion.
Autoboxing: The mechanism of converting the primitive type into the reference type or objects is known as Autoboxing.
Unboxing: The mechanism of converting the reference type or objects into the primitives is known as unboxing.
Difference between Primitive and Reference Data Types
Some important differences between the Primitive and Reference types are listed below.
Sr.No. | Reference Type | Primitive Type |
The Reference data types are not pre-defined except the String data type. | All the primitive data types are pre-defined in Java. | |
The Reference data type can be utilized to call the functions. | The primitive data type cannot be utilized to call the functions. | |
The Reference data type is, by default, null. | The primitive data type cannot be null. | |
They hold the address or reference of the value, not the actual value. | They store the actual value. | |
The size allocated by the JVM to all the reference data types by default is 8 bytes. | The size of primitives depends on the data type, and each primitive data type has a different size. | |
The Reference data type always starts with uppercase letters. | The primitive data types always start with lowercase letters. | |
Examples: String, Interface, Enum, Annotations, Array | Examples: int, double, float, long, byte, char, short. |