0

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++?

6
  • Difference in C++ - In what sense? You already posted that C++ uses both pass by value and pass by reference. Commented Nov 22, 2012 at 14:56
  • 2
    The difference in C++ is that you can pass by reference, so you can change a value in the calling function from the called function. Commented Nov 22, 2012 at 14:57
  • 2
    @IshanKhanna flagging isnt required, vote for close if you like :) Commented Nov 22, 2012 at 15:02
  • 4
    just because of @T.J. Crowder's answer, this question should not be downvoted Commented Nov 22, 2012 at 15:19
  • 1
    @Jerome Agreed. I've tried to tidy up the question to make it more useful down the road. Commented Nov 22, 2012 at 15:26

4 Answers 4

8

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:

void foo() { int a = 42; bar(a); System.out.println("foo says: " + a); } void bar(int a) { a = 67; System.out.println("bar says: " + a); } 

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:

void foo() { int a = 42; bar(a); cout << "foo says: " << a; } void bar(int& a) { a = 67; cout << "bar says: " << a; } 

Note that bar is defined as receiving a reference (int& a). If you call bar, you see:

bar says: 67 foo says: 67

bar was able to change the value of a within foo.

Okay, so let's deal with object references: First off, note that the word "reference" is being used for two completely different things: A reference to the variable in the calling function (that's the pass-by-reference thing), and a reference to an object. When you pass an object reference into a method in Java, the reference is passed by value, just like everything else is.

void foo() { List list = new ArrayList(); List ref2 = list; // (Let's remember that object reference for later...) bar(list); System.out.println("foo says: " + list.size()); System.out.println("foo says: Same list? " + (ref2 == list)); } void bar(List list) { // `bar` can modify the state of the object the reference points to list.add(new Object()); System.out.println("bar says (after add): " + list.size()); // ...but cannot change `foo`'s copy of `list` list = new ArrayList(); System.out.println("bar says (after new): " + list.size()); } 

So you see:

bar says (after add): 1 bar says (after new): 0 foo says: 1 foo says: same list? true

bar can change the state of the object whose reference was passed in (by value), but cannot change foo's reference to that object. foo doesn't see the new list bar created.

Sign up to request clarification or add additional context in comments.

Comments

2

In Java you can pass primitives and reference by value. There are no other options.

In C++, you can pass primitives and pointers by value or by reference. If you pass by reference, youc an see changes made to the value or pointer after it was passed to the method and you can change the original value.

1 Comment

You can see changes made to the value of the pointer passed to a function for example (e.g. you had a pointer passed in that referred to memory address A, and after function ends the pointer could refer to address B)
1

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:

void foo() { int a = 42; bar(a); System.out.println("foo says: " + a); } void bar(int a) { a = 67; System.out.println("bar says: " + a); } 

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:

Comments

0

In C++ something equivalent to Java is this

void Swap(Type* A, Type* B) { // here we have pointers to type A (in Java they are called references and don't use * syntax) A->DoThis() // can call with -> a method on object //if you try to swap A with B not possible because we don't have A*/B* reference // see bellow how to do this } 

The way to handle limitation from above in C++ is

void Swap(Type& A, Type& B) { Type& temp = A; A = B; B = temp; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.