Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java vs C++ for passing arguments

Ive started to get my head in a bit of a mix regarding how Java and C++ pass arguments.

Is this correct:

Java passes using call by value, but the value is actually the reference (not the actual data/object). So a copy of the address is made?

C++ by default, passes by value, but the value is not the reference, its the actual data. If you want to simular real call by reference use & or a pointer?

In summary, Java makes copies of parameters, but it's a copy of the reference. C++ usually makes a copy but not of the reference (unless you use & or pointers), of the actual underlying data?

like image 352
mezamorphic Avatar asked Jun 20 '12 15:06

mezamorphic


4 Answers

C++(03) always makes a copy unless you pass by reference. (Theoretically... in practice, copy elision can occur, but it's irrelevant in regards to the question)

If you pass by pointer, you still make a copy (granted, it's a copy of the pointer, but still a copy).

like image 155
Luchian Grigore Avatar answered Nov 07 '22 07:11

Luchian Grigore


Your summary is correct.

I personally find simpler, when comparing Java with C++, to consider Java as the equivalent of C++ when non-primitive types are passed as pointers by value. In Java you just do not see, and cannot manipulate, the pointer.

like image 31
Luca Geretti Avatar answered Nov 07 '22 08:11

Luca Geretti


The summary for Java is correct. Indeed, it also applies to non-reference types.

like image 1
Stephen C Avatar answered Nov 07 '22 07:11

Stephen C


Well, in both Java and C++, there are two options when passing arguments: Pass by Value(PbV) and Pass by Reference(PbR). Java pass all preliminary objects by value, and pass all other objects by reference. While C++ pass everything by value unless you require to pass by reference.

  1. Pass by Value

In both Java and C++, pass by value is to make a copy of the passed object, what you do in the function to the object will not have any influence on the original object outside the function. e.g.,

void foo(aTemp)
{
aTemp = 2;
}
....
int a = 1;
foo(a)
std::cout << a;

The output will be 1;

  1. Pass by Reference

Pass by reference does not make a copy of the object, it makes a copy of the reference of the object. e.g.

void foo(&aTemp)
{
aTemp = 2;
}
....
int a = 1;
foo(a)
std::cout << a;

The output will be 2.

  1. More Complicated Situation

So far, there is no big difference between java and c++. However, if you assign passed-in object to a new one, the situation will be different. e.g. in C++,

 void foo(&aTemp)
 {
 myClass b(2);
 aTemp = b;
 }
 ...    
 myClass a(1);
 foo(a)
 std::cout << a;

The output will be 2.

However, in Java

void foo(aTemp)
{
myClass b = new myClass(2);
aTemp = b;
}
...
myClass a = new myClass(1);
foo(a)
std::cout << a;

The out put will be 1.

The different output here is caused by the difference in "=" in java and c++.

In c++, if you do not override "=" in a irregular way, the value of myClass b will be copied into aTemp which points to a.

While in Java, when you do aTemp = b, you assign the address of object b to aTemp, so aTemp no longer points to the original a, and no matter what you do to aTemp, it has no long influence on a.

So in summary, all arguments in Java and C++ are passed by value -- value of the object or value of the reference -- the difference only lays on how you manipulate the value.

This is what I learned from my normal work, hope this helps.

like image 1
Kun Avatar answered Nov 07 '22 06:11

Kun