Java uses pass-by-value, both with Objects and primitive types. Because Java passes the value of the reference, we can change the value of target but cannot change the address.
How does this compare with C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference is whether you can affect a variable in the calling function.
Let’s leave objects aside for a moment. In Java, which is purely pass-by-value, you can’t change the calling function’s value of a variable passed into a called function. Example:
If you call foo, you’ll see:
bar says: 67
foo says: 42
bar could not change foo’s a.
That’s true in C++ if you pass by value. However, if you pass by reference, you’re passing a reference to the calling code’s variable. And that means the called code can change it: