After getting familiar with java programming fundamentals one with programming mind always thinks about 'is java pass by value or reference '. Throughout this blog we would be able to conclude that java is pass by value. Java is always pass by value, often confusion occurs when Object are passed into method as arguments.
Pass by value: Make a copy in memory of the actual parameter's value that is passed in. For eg: suppose you hava file1.txt in local disk, let say 'D' drive, copying file1.txt into Desktop and changing it doesn't change the content inside the file1.txt of 'D' drive.
Pass by reference: pass a copy of the address of actual parameters. For eg: suppose you created a shortcut file for file1.txt in desktop then changing contents from shortcuts also changes file in 'D' drive.
Pass by value: Make a copy in memory of the actual parameter's value that is passed in. For eg: suppose you hava file1.txt in local disk, let say 'D' drive, copying file1.txt into Desktop and changing it doesn't change the content inside the file1.txt of 'D' drive.
Pass by reference: pass a copy of the address of actual parameters. For eg: suppose you created a shortcut file for file1.txt in desktop then changing contents from shortcuts also changes file in 'D' drive.
Above program prints 9, from which means copy of value 9 is sent to the argument for test function due to which printing value of x in main method gives result 9.
But
Credential class for accessor and mutator |
Since this program prints "admin" in console, we think it's pass by reference, but it is also pass by value since the value of address (copied reference) is passed as argument.
furthermore object 'cred ' at line number 6 is original reference and 'cred ' at line number 7 inside test method is copied reference(copied from original reference)
Since the original and copied reference refer the same object, the member value gets changed.
Comments
Post a Comment