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 - Java Tutorial
From the course: Java 8+ Essential Training: Syntax and Structure
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Evaluate conditions with if-else4m 17s
-
Evaluate conditions with switch-case4m 32s
-
Create looping code blocks7m 13s
-
Create reusable code with methods8m 17s
-
Create overloaded methods3m 27s
-
Pass arguments by reference vs. value4m
-
Challenge: A more complex calculator1m 18s
-
Solution: A more complex calculator2m 20s
-
-