There is no Pass-by-reference concept in java, everything pass-by-value.
Java only supports pass by value.
Java copies and passes the reference by value, not the object. The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution.
Lets observe the java example :
We are passing Student object reference to another Method(modifyScore), modifying the score value in method, but out put will be the modified value not actual value. why?
Because the std was pointing to the object Student. The std got passed by value into the method. So a copy of the reference got passed into the method. But a copy of a reference still pointing to the same object. So changes to the std in the method are felt outside the method as well.
Java only supports pass by value.
Java copies and passes the reference by value, not the object. The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution.
Lets observe the java example :
public class Student { int score; public int getScore() { return score; } public void setScore(int score) { this.score = score; } public static void main(String[] args) { Student std= new Student(); std.setScore(20); std.modifyScore(std); System.out.println("Score after pass by value : "+ std.getScore()); } public void modifyScore(Student std){ std.setScore(10); } }Out put : Score after pass by value : 10
We are passing Student object reference to another Method(modifyScore), modifying the score value in method, but out put will be the modified value not actual value. why?
Because the std was pointing to the object Student. The std got passed by value into the method. So a copy of the reference got passed into the method. But a copy of a reference still pointing to the same object. So changes to the std in the method are felt outside the method as well.
No comments:
Post a Comment