From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Passing arguments by reference or by value

Passing arguments by reference or by value - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Passing arguments by reference or by value

- One of the questions you have to answer when you learn a new programming language is whether arguments passed into a method are being passed by copy, or by reference. Let's define these terms. When you pass arguments by copy, that means you're passing a variable into a function or a method, and within the method, a copy of the variable is received. It's not attached in any way to the original value before it was passed in. And when you pass into a method by reference, that means you're referring to the original object, the object that existed outside the method, and if you make any changes to the object within the method, it'll affect whatever was passed in originally. In Java, variables are always passed into methods by copy. But there are certain behaviors that can make you think you're passing by reference. I'm going to describe three scenarios, and explain how each of them proves the point that arguments are passed by copy. Let's take the simplest scenario, passing primitive…

Contents