0
What is a reference variable in Java?How does it is different from other variables?
2 Réponses
+ 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.
+ 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.