Is Java Pass by Reference or Pass by Value?
- The Java Spec says that everything in Java is pass-by-value.
- There is no such thing as “pass-by-reference” in Java.
These terms are associated with method calling and passing variables as method parameters. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of complex types.
- In java, when we pass a reference of complex types as any method parameters, always the memory address is copied to new reference variable bit by bit.
- In above example, address bits of first instance are copied to another reference variable, thus resulting both references to point a single memory location where actual object is stored.
- Remember, making another reference to null will not make first reference also null. But, changing state from either reference variable have impact seen in other reference also.