1

I know how to use a variable and work them. However, it is very confusing to understand what variables really are because of many different definition given out there. What really confused me was How java's objects behave like pass by reference even though it is pass by value. So pass by reference is when the called function creates pointers to its arguments and pointers points to a variable address, right? But in java, the address of the object gets passed to the function which makes it behave like pass by reference. does that mean variables and and value are not the same thing? it seems from the above explanation they have different memory address. when a pointer to variable is passed it is passed by reference. but when the memory address of an object is passed it is passed by value.

int obj = new SomeObject(); // do obj and SomeObject have the same memory address...confused

I know the difference between pass-by-value and pass-by-reference. I just want to know if a variable actually maps to the memory location or to the memory address or to the value stored in that memory location/address?

aziz
  • 83
  • 1
  • 7
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – ardila Jun 27 '18 at 10:51

1 Answers1

1

C and Java passes everything by value, there is no other way. That is also the default for C++, but it has extra notation for passing arguments as references.

Passing something by value means that a new variable is created and it is internal to the method/function:

// C/C++, Java
void something(int x){
  x=10;
  // printf/cout/System.out.println x here, and it will be 10
}

void usesSomething(){
  int y=5;
  // printf/cout/System.out.println y here, and it will be 5
  something(y);
  // printf/cout/System.out.println y here, and it will be 5, x was just a copy
}

And Java objects are not different:

void something(String x){
  x=new String("something");
  System.out.println("In something: "+x);
}

void usesSomething(){
  String y=new String("Hello!");
  System.out.println(y); // "Hello!"
  something(y);
  System.out.println(y); // again "Hello!", x was just a copy
}

The confusing part is the wording only, as the variables themselves are called "object references" in Java (they would be "pointers" in C/C++). But they (the object references) are passed by value, nothing happens to y in this code.

Passing something by reference means that doing anything with the reference will directly affect the original variable.

// C++ only
void something(int &x){
  x=10;
}

void usesSomething(){
  int y=5;
  std::cout << y << std::endl; // it is 5
  something(y);
  std::cout << y << std::endl; // it is 10, x was the same variable as y
}


One thing which may be worth mentioning is that an object and a variable are never the same thing in Java. Objects reside on the heap (like the deliberately exaggerated "Hello!" and "something"), while the reference variables to these objects (x and y) can reside anywhere (here they reside on the stack), and they have their own memory for storing the reference to these objects (in C/C++ it would be very much like a pointer, which also has a size, something like 4-8 bytes).
So in the general case this is how object can 'feel' being passed by reference: you actually pass the object reference by value, ending up referring the same object. If the object is mutable (Strings are not), changes will be visible from the outside, via any other variable referring the same object. But the reference variable itself is your own, if you change it (and refer to a different object, perhaps null), no one else will notice.
tevemadar
  • 12,389
  • 3
  • 21
  • 49