Net-informations.com
SiteMap  | About    

Does Java pass by reference or pass by value?

First of all we should understand what is meant by pass by value or pass by reference.

passed by reference

When a parameter is passed by reference , the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller's variable.

passed by value

When a parameter is passed by value , the caller and callee have two independent variables with the same value. If the callee modifies the parameter variable, the effect is not visible to the caller.

When passing by ref you are basically passing a pointer to the variable. Pass by value you are passing a copy of the variable. In basic usage this normally means pass by ref changes to the variable will see be the calling method and pass by value they won’t.

The Java Spec says that everything in Java is pass-by-value . There is no such thing as "pass-by-reference" in Java. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners .

Example

Output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.