Understanding Reference data type
Reference data type for class object reference and array reference.
Learning Objectives
- Understanding class object as a reference data type.
- Understanding array name as a reference data type.
Source Code
|
Run Output
Code Understanding
class RefDataType
{
int rd=5;
This is declared outside main as it would be referred using the class object.
public static void main(String[] args)
{
RefDataType robj = new RefDataType();
Here robj is simply a reference to the memory area reserved for class based references.
System.out.println(robj.rd);
The reference data type has been used to reference to primitive data in the reference object.
int ar[]={1,2};
Here the array name ar is also a reference to memory location.
System.out.println(ar);
Here the memory location reference is referred so you may get some output which is different in each case as a different location would be referred to.
System.out.println(ar[0]+”, “+ar[1]);
Here the actual value at the given location pointed by 0th and 1st location in the memory reference are being printed.
}
}
Notes
- The oracle documentation for reference type and other types can be seen at.
https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
Suggested Filename(s): RefDataType.java
sunmitra| Created: 20-Mar-2018 | Updated: 20-Mar-2018|