From the course: Java 11+ Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Pass arguments by reference vs. value - Java Tutorial
From the course: Java 11+ Essential Training
Pass arguments by reference vs. value
- In any programming language, you have to note when you pass arguments to methods, whether they're being passed by copy or by reference. Let's define these terms. When you pass arguments by copy, you're passing an argument in and receiving a copy of that value. You're not referencing the original version. When you pass by reference, you're receiving a reference to the original object, and if you change anything within the method, you'll change the original. In Java, arguments are always passed by copy, but it may look like something else is happening, and I'll explain why. Let's start with the simplest case, primitive values. My method increment value receives a single argument, an integer value, a primitive. Within the method it increments that value by 1, and outputs that value. Now in the calling code, I establish an original variable, I output its value, I call the method, and then I output the original…