This is one of the most confusing topics in Java .The main aim of this post is to solve the confusion in the most simple way possible.
Before going into this discussion, first of all we need to understand the meaning and difference between Pass By Value and Pass By Reference?
Before going into this discussion, first of all we need to understand the meaning and difference between Pass By Value and Pass By Reference?
- Pass By Value : This concept says that whenever a variable is passed to a method/function , a copy of this variable is passed not the actual variable. So the changes that are made to the variable in method are not reflected in the actual variable.
- Pass By Reference : This concept says that whenever a variable is passed to a method/function, actual variable is passed. So the changes that are made to the variable in method are reflected in the actual variable.
Since we have got the basic understanding, lets look at this concept from Java's perspective. In Java there is concept of primitives and non-primitives . Primitives are the basic data types which store the value directly. Non-primitives are defined by programmer and are used as reference to the objects. These references store the memory location of the object to which they refer.
Now let us clear the confusion. JAVA is Pass By Value as simple as that .We will see this HOW ?
Now let us clear the confusion. JAVA is Pass By Value as simple as that .We will see this HOW ?
We will create a object of this class in our main class with id 1 and name as "Student1" and assign this to reference std. Let us assume this object is created at memory location 200. So the reference std is pointing to the memory location of this object .
Output is :
The output of line 12 is : Std id is : 1 Std name is: Student1 (object at location 200)
When std is passed to method modify at line 16, basically it is the reference value that is being passed which in this case is the memory address of this object 200.So now this object has two references , one is object reference std and other is method reference student.
At line 23, student reference is pointing to object at memory address 200 , which is the original object created at line 8, so the name of this object is updated to Student2 at line 23.
At line 24, a new student object is created with id:2 and name : Student3. Suppose this object is created at memory location 300 , so the reference 's' points to memory location 300.
At line 25, s is assigned to student . It means value of s i.e the memory location is assigned to reference student . So now student reference points to object at memory location 300.
This leads to output at line 27 : Std id is : 2 Std name is: Student3 (object at location 300)
Now at line 18 , the reference std is still pointing to object at address location 200 , so we get the output as : Std id is : 1 Std name is: Student2 (object at location 200)
This proves that JAVA is Pass By Value .
I hope this gives enough understanding about this topic .If you like this post please do share it !
Main Class |
The output of line 12 is : Std id is : 1 Std name is: Student1 (object at location 200)
When std is passed to method modify at line 16, basically it is the reference value that is being passed which in this case is the memory address of this object 200.So now this object has two references , one is object reference std and other is method reference student.
At line 23, student reference is pointing to object at memory address 200 , which is the original object created at line 8, so the name of this object is updated to Student2 at line 23.
At line 24, a new student object is created with id:2 and name : Student3. Suppose this object is created at memory location 300 , so the reference 's' points to memory location 300.
At line 25, s is assigned to student . It means value of s i.e the memory location is assigned to reference student . So now student reference points to object at memory location 300.
Now at line 18 , the reference std is still pointing to object at address location 200 , so we get the output as : Std id is : 1 Std name is: Student2 (object at location 200)
This proves that JAVA is Pass By Value .
I hope this gives enough understanding about this topic .If you like this post please do share it !
Explained in simple way.
ReplyDeleteThanks Vaibhav.I hope this helped
DeleteIf you like this post please do share it
DeleteGood work Bhavya!
ReplyDeleteThanks Naresh !!
DeleteIf you like this post please do share it
DeleteSimplified the concept, very nicely put across.
ReplyDeleteThanks Rachit. There are more to come
DeleteIf you like this post please do share it
DeleteWell explained, Concise & simple way to guide
ReplyDeleteThanks Rohit. Your comments and feedback is always helpful.
DeleteIf you like this post please do share it
DeleteHi bhavya, good explanation for pass by value and pass by reference. Also could you please explain how the hashmap, hashtable and hashset works internally
ReplyDeleteThanks Bala..Sure I am planning include the hashing and internal hashmap working in my subsequent blogs. Would update you once its ready
DeleteIf you like this post please do share it
DeleteA very thorough and good explanation Bhavya.
ReplyDeleteThanks , If you like this post please do share it
DeleteGood concept. Really good for interview also. To crack a java interview for any fresher on this topic, We can look deeply into this blog. Good Job Done!!
ReplyDeleteThanks for your feedback.Please do share it.
Delete