0

What is a reference variable in Java?How does it is different from other variables?

11th Jan 2020, 6:36 AM
SATYAM SINGH
SATYAM SINGH - avatar
2 Respuestas
+ 2
Reference variables are associated with objects and arrays, they point to the reference of the object. For example when you pass an object to a method the reference to the object is passed not the actual object itself so any modification of the object in the method modifies the same object you passed that's why the variable you passed is changed since they point to the same object. In value variables you work with the actual value, when passed to a method the actual value is the one passed so any modification in the method does not affect the initial variable.
11th Jan 2020, 7:28 AM
Jahnics
Jahnics - avatar
+ 2
Student s1 = new Student(); Here s1 is a reference variable which is referring to the object of type Student. The new Student() is the object which is created in the heap memory and s1 is created in stack which is pointing the object.
11th Jan 2020, 10:54 AM
Avinesh
Avinesh - avatar