So we all know java .reference .It is an address that indicates where an object’s variables and methods are stored. Java spec says :
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object
And most common example given is something like below:
@author: åminur ------------------------------------------------------ PART 1: Assignment of object, points to reference. ------------------------------------------------------ 1. Emp tmpEmp = new Emp(); 2. Emp e1 = tmpEmp; +---------------------------+ | | +---------------------------+
However surprisingly, recently I found that a few are not aware of the 2nd part of it. They assume any change in tempEmp will always reflect in e1, even when tempEmp’s reference has been nulled/or changed. Something like this this.
-------------------------------------------------------------- PART 2 : Which a couple of folks couldn't answer in interview. -------------------------------------------------------------- 3. tmpEmp = new Emp(); // or tmpEmp=null; +---------------------------+ tmpEmp --------> |object created at line 3 | +---------------------------+ +---------------------------+ e1 ------------> |object created at line 1 | +---------------------------+
Related posts
Feed
- India
- World
- Live
- Cricket News
This site uses cookies. These cookies come mainly from third party library used at this site(Google/Jetpack/etc). By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here Cookie Policy
To find out more, including how to control cookies, see here Cookie Policy