Before We Started with the Reference variable we should know about the following facts. Show 1. When we create an object(instance) of class then space is reserved in heap memory. Let’s understand with the help of an example.
Now, The space in the heap Memory is created but the question is how to access that space?. Then, We create a Pointing element or simply called Reference variable which simply points out the Object(the created space in a Heap Memory). Understanding Reference variable 1. Reference variable is used to point object/values. 2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java. 3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value. 4. You can access object members using a reference variable using dot syntax.
Example: Java
Output Demo@214c265e x = 10 0 Let us see what is actually happening step by step. 1. When we create an object of demo class new DEMO();, the default constructor is called and returns a reference of the object, and simply this reference will be stored to the reference variable D1 (As we know that associativity is Right-hand side to left-hand side). 2. The value of a reference variable is a reference. When we attempt to print the value of a reference variable, the output contains the type of the variable and the hash code created for it by Java: the string Demo@214c265e tells us that the given variable is of type Name and its hexadecimal format of hash code is 214c265e. 3. At this point we will access the methods display() of the class demo using our custom reference variable that we created.
Java
Java
Java
More on Reference Variable1. Reference Variable as Method Parameters: As the value of a primitive variable is directly stored in the variable, whereas the value of a reference variable holds a reference to an object. We also mentioned that assigning a value with the equality sign copies the value (possibly of some variable) on the right-hand side and stores it as the value of the left-hand-side variable. A similar kind of copying occurs during a method call. Regardless of whether the variable is primitive or reference type, a copy of the value is passed to the method’s argument and copied to that argument.
But we know that the reference variable holds the reference of an instance(OBJECT) so a copy of the reference is passed to the method’s argument. Example: Java
Output x = 95 y = 20 x = 10 y = 55 SCENE 1 : SCENE 2: Now, What is going on here, when we pass the reference to the method it will copy to the reference variable defined in the method signature and After that, they also have access to the object members. Here, We defined two instances named C and D. Afterwards we pass C and D to the method which further gives reference to A and B At Point 1: A will update the value of x from 10 to 95, hence C.display() will show 95 20 but in another object D we update the value of x through D only from y =20 to 55, hence D, display() will show 10 and 55.
2. What if we swap the reference variables with the help of the Swap Method? The fact is if we try to swap the reference variable, then they just swap their Pointing element there is no effect on the address of reference variable and object(Instance) Space. Let’s Understand It with the help of an example: Java
3. What if we pass arrays to the method will it be able to update the Actual Array’s values, even we know that a copy of the array is a pass to the formal Array? The answer is YES, the values will be updated by Formal parameter, The Fact is, When we create an Array, a memory is assigned to the array of the desired size, and it returns the reference of the first array’s element that is the base address that will store to the Formal Array(Method argument). As we learned earlier every pointing reference variable can change or update the object. Example: Java
Output 1 , 2 , 3 , 4 , 5 , 1 , 2 , 99 , 4 , 77 , 4. this and super keywords are also Pointing Elements. this keyword. In java, this is a reference variable that refers to the current object. super is used to refer immediate parent class instance variable. We can use the super keyword to access the data member or field of the parent class. It is used if parent class and child class have the same fields. 5. null value of a reference variable.
A. The null reference can be set as the value of any reference type variable. B. The object whose name is obj is referred to by nobody. In other words, the object has become garbage. In the Java programming language, the programmer need not worry about the program’s memory use. From time to time, the automatic garbage collector of the Java language cleans up the objects that have become garbage. If the garbage collection did not happen, the garbage objects would reserve a memory location until the end of the program execution. Java
Output Exception in thread "main" java.lang.NullPointerException at Main.main(File.java:17) Java Result: 1 Here, we try to access objects’ members by a reference variable that is pointing nothing(null), and hence it shows NullPointerException. Now if you get the error, the first step is to look for variables whose value could be null. Fortunately, the error message is useful: it tells which row caused the error. Just try it out yourself! What is object reference in Java?A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.
How do you declare a reference variable in Java?There are several ways to declare a reference variable:. ClassName variableName;. ClassName variableName = new ClassName( parameter, parameter, ... ) ;. ClassName variableNameOne, variableNameTwo ;. How do you declare an object's reference variable?Declaring a Variable to Refer to an Object
Previously, you learned that to declare a variable, you write: type name; This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.
Where are reference variables stored in Java?All objects in Java are stored on the heap. The "variables" that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also. The Class objects that define Classes are also heap objects.
|