0

Is the following statement about Java true of false?

In Java, when an instance of a class, or object, is specified as a parameter to a method, a copy of that object is being made

I know that functions in Java are pass-by-value, which would mean that a copy of the object is being made?

But at the same time, if java objects are references and you pass a reference, this is different to a copy of the actual data isn't it?

And if your passing a reference, when the reference is reassigned the object would be reassigned, making Java pass-by-reference not pass-by-value?

As you can see I am VERY confused about this

William
  • 109
  • 1
  • 2
  • 7
  • Here is a pretty complete [question](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1) with examples. A copy of the object isn't being made, a copy of the reference is being made. – matt Jun 15 '16 at 07:58

5 Answers5

8

In java everything is passed by copy.

Primitives are passed by copy. So changing it inside the function is not reflected outside.

For objects what is passed to the function is a copy of the reference (not a copy of the object). It means that changing a property inside the function the external reference see the modification, but changing the reference itself (for example assign null) is not reflected outside the function.


Follow some example to explain better.

Function changing a primitive:

public void notChange(int a) {
    a = 3;
    // Here a is 3
}

int a = 0;

notChange(a);
// Here a is 0

For a function changing inner content of an object

public void changeContent(List<String> list) {
    list.add("x");
    // Here list has one more element
}

List<String> list = new ArrayList<String>();
// Here list has size 0
changeContent(list);
// Here list has size 1

For a function changing the reference of an object

public void changeReference(List<String> list) {
    list = null;
}

List<String> list = new ArrayList<String>();
changeReference(list);
// Here list is not null
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

In case of objects, a copy of the reference is passed. Please note that this does not mean that a copy of the object itself is passed. Any changes you make to the object will also reflect in the original object. This could lead to some crazy sh*t if you do not copy your objects properly. Use a copy constructor or something similar to copy objects.

Also, read about deep and shallow copy.

Community
  • 1
  • 1
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
0

Here is an example of pass by value and pass by reference in c++. In this example, java behaves like the pass by value version. The value is the pointer to the object.

#include <stdio.h>

class Obj{
  int value;
  public:
    Obj(int v){
        value = v;
    }
    void echo(){
        printf("%d\n", value);
    }
};

void byValue(Obj* obj){
    obj = new Obj(-1);
    obj->echo();
}

void byReference(Obj*& obj){
    obj = new Obj(-1);
    obj->echo();
}

int main(int argc, char** args){
    Obj* o = new Obj(1);
    byValue(o);
    o->echo();
    byReference(o);
    o->echo();
    return 0;
}

(Sorry about the leaks, it is not an example of good practice) The output is:

-1 
1
-1
-1
matt
  • 10,892
  • 3
  • 22
  • 34
0

I will try to answhere you step by step starting from your first question:

In Java, when an instance of a class, or object, is specified as a parameter to a method, a copy of that object is being made

This is false, when you pass an object to a method Java never make a copy of it, as Davide Lorenzo said you are passing the reference to the object, not the object itself so if the method modify some of the attribute of the class you will have the value modified also outside from the method.

So we can answer to the second and third questions:

I know that functions in Java are pass-by-value, which would mean that a copy of the object is being made?

No, Java doesn't make a copy. As Davide Lorenzo said you are passing a copy of the reference not a copy of the object.

But at the same time, if java objects are references and you pass a reference, this is different to a copy of the actual data isn't it?

Yes, passing the reference is really different to passing a copy of the object. I will clarify everything with an example. Let's say we have the object Flower with the attribute color

public class Flower{
    String color = "red";
}
enter code here

We can imagine the method setColor of AnotherObject

public void setColor(Flower flower, String color_string){
    flower.color = color_string;
}

We can have the following situation

public static void main(String args[]){
   Flower myFlower = new Flower();
   AnotherObject otherObject = new AnotherObject();

   otherObject.setColor(myFlower, "yellow");
   System.out.println("The color of the flower is: "+myFlower.color);
}

The output of this code will be:

The color of the flower is yellow

This is why you are passing the reference of the object, not the object itself.

Now you can ask why Java is considered pass-by-value? There is a very interesting discussion on this previous post about this, I would cite the best answer of it:

Java is always pass-by-value. Unfortunately, they decided to call pointers references, thus confusing newbies. Because those references are passed by value.

Last but not least note that only primitive type are not managed trough the references in Java, so passing an int is really different from passing an Integer, I suggest you to take a look on this post about the argument.

Community
  • 1
  • 1
Niles
  • 489
  • 1
  • 8
  • 18
0

In Java, when an instance of a class, or object, is specified as a parameter to a method

This is where you are going wrong. Each parameter has a type. The only types in Java are primitive types and reference types. So the value of a parameter (or any other variable, or value of any expression) in Java can only be a primitive, or a reference. Not an "object".

newacct
  • 119,665
  • 29
  • 163
  • 224