Everyone says that Java goes by value, including Objects. These are passed by copy and can not be reassigned to a new object within the method. The problem is that it is possible to change the value of objects within the method. Does not this characterize pass-by-reference simulation (made with C ++ pointers)?
Java Code Example
class Objeto {
public int valor;
public Objeto(){
this.valor = 2;
}
}
public class Passagem {
public static void main(String[] args) {
Objeto obj = new Objeto();
System.out.printf("Valor inicial: %d\n", obj.valor);
metodo(obj);
System.out.printf("Valor após o método: %d\n", obj.valor);
metodoNovo(obj);
System.out.printf("Valor após o método com novo objeto: %d\n", obj.valor);
}
// Simula a passagem por referência alterando o valor do objeto
// mas não o local de memória para qual aponta
static void metodo(Objeto obj){
obj.valor = 5;
}
//Tenta alterar o objeto em si
static void metodoNovo(Objeto obj){
obj = new Objeto();
obj.valor = 10;
}
}
The value of the object can be directly changed by the first method. Already in the second it is not possible to reassign it to a new object, even because the method expects to receive a constant object - to change an object itself logic says that we would have to have an object for an object .
The example in C ++, which behaves in the same way:
Code Example in C ++
#include <iostream>
using std::cout;
using std::endl;
class Objeto{
public:
int valor;
Objeto(){
this->valor = 2;
}
};
void funcao(Objeto *obj){
obj->valor = 5;
}
void funcaoNovo(Objeto *obj){
obj = new Objeto();
obj->valor = 10;
}
int main()
{
Objeto *obj = new Objeto();
cout << "Valor inicial: " << obj->valor << endl;
funcao(obj);
cout << "Após função: " << obj->valor << endl;
funcaoNovo(obj);
cout << "Após função que atribui novo objeto: "
<< obj->valor << endl;
}
The result of the two codes is the same.
It is only possible to change the object itself in C ++ code if the function expects to receive the address of the object itself, as in:
C ++ Function Changing Object
void funcaoNovo(Objeto **obj){
*obj = new Objeto();
(*obj)->valor = 10;
}
main for the above function
int main()
{
Objeto *obj = new Objeto();
cout << "Valor inicial: " << obj->valor << endl;
funcao(obj);
cout << "Após função: " << obj->valor << endl;
funcaoNovo(&obj);
cout << "Após função que atribui novo objeto: "
<< obj->valor << endl;
}
Where you can see that the reallocation worked out.
Based on these comparisons, is it safe to say that in Java there is also a passing-through simulation ? You can not change the object (the location it points to), but you can change its values because the calling function (or method) directly changes the value of the values in the objects.
Note: In C you can also do the same test. A given function funcao(int *ptr)
can not assign ptr
to a new malloc
within it. Only the value can be changed and is considered a passing simulation by reference.
My biggest question:
In C ++ the object itself does not change the original value.
void funcaoNovo(Objeto *obj){
Objeto *novo = new Objeto();
novo->valor = 10;
obj = novo;
}
Here the obj
is not changed outside the function. Just like in Java.
In other words, you can not reassign an object either because C ++ can not change the object passed by copy as well. However you can change the value contained in the object (here is the simulation), as long as you do not try to change the object.
This is called passing simulation by reference, because the calling function modifies the data contained in Object and not the Object itself. Java does not have reference passing (except with arrays), but would not it be correct to say that there is a simulation of this passage in the value for which the object reference , ie I get a simulation when referencing the values?
I see that both passages are similar - they allow the function to change values out of scope (including multiple values), without allowing pointer reassignment (Java reference).