20 October 2010
0 What is Reference variables in Java?
Reference variables in Java
Reference variables are used in Java to store the references of the objects created by the operator new
Any one of the following syntax can be used to create a reference to an int array.
int x[];
int [] x;
int [] x;
The reference x can be used for referring to any int array.
//Declare a reference to an int array
int [] x;
//Create a new int array and make x refer to it
x = new int[5];
int [] x;
//Create a new int array and make x refer to it
x = new int[5];
The following statement also creates a new int array and assigns its reference to x
int [] x = new int[5];
In simple terms, reference can be seen as the name of the array. Arrays can be created only using dynamic memory allocation in Java. The memory is allocated dynamically using the keyword new.A reference type can be assigned ‘null’ to show that it is not referring to any object. ‘null’ is a keyword in Java
int [] x = null;
Subscribe to:
Post Comments (Atom)
0 comments:
Please give your valuable comments.