Understanding Reference data type – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3859    siteicon   siteicon  

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

TC++ #3859

Source Code

class RefDataType
{
int rd=5;
public static void main(String[] args)
{
RefDataType robj = new RefDataType();
System.out.println(robj.rd);
int ar[]={1,2};
System.out.println(ar);
System.out.println(ar[0]+", "+ar[1]);
}
}

Test it !

Run Output

5
[I@16c85db
1, 2

Note: The second line could be different in your case as it is just a memory reference

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


Suggested Filename(s): RefDataType.java



Share

sunmitra| Created: 20-Mar-2018 | Updated: 20-Mar-2018|






Back