From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

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

Pass arguments by reference vs. value

Pass arguments by reference vs. value - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Pass arguments by reference vs. value

- [Instructor] Whenever you're learning a programming language, you always have to ask the question when I create a variable and pass it to a function or a method, am I creating a copy of that variable or am I using a reference to the original variable? Every language has a different set of answers to this question, but in Java it's very consistent. When you pass a variable, you're always passing by copy. That is the method receives a copy of the variable. But there's an important wrinkle here. We're talking about the variable itself and not the object that the variable is referencing. That is a completely different question. When you pass primitive values, you're always creating a copy. In this example I have a method called increment value which receives an integer. It increments the value by one and then prints that value. Now when I call the method I start with an original value, I output that original value, then I call the increment value method and I print out the original…

Contents