Hello, well this suddenly surprised me .. dunno why ... i was trying to solve the binary-tree implementation ... where i noticed using root.left = new node bla bla

My question
insert(Node root,int value)
root.left = new node(null null value)
Etc ...

The function doesnt create a copy of the root ... it will use the passed object right ?
Can one tell me what type of parameters a fnctn creates a copy and which doesnt ?
Ex:
String - fnctn will create a copy of it .. so any changes made inside the fnctn to the string passed a parameter will not affect the original string ...
Thank u :)

All parameters work the same way, passing the value of the argument. That means you always get a shallow copy, but when the value is a reference to an object, that means nothing more than a copy of a reference, not a new reference to a copy of the object. If you know C language, then think of all Java object reference types as implicit pointers where the * is hidden. Then argument passing in Java works exactly the same way it does in C language.

When you pass a String reference to a method, the method gets a copy of the reference so it can use the same String that you were using with your reference. If the method somehow managed to change the String, your reference to that String would cause you to see the changes the next time you used the String. Fortunately, String is designed to make changes impossible.

Edited 3 Years Ago by bguild

Dear bguild, thanks a lot for your reply, I think what you wrote is very clear for a C programmer, I have read some C code and learn some but I didn't started with pointers chapter yet. Anw, can I say that passing an object is like passing an array. In other words, passing an array and changing its content will affect the declared array at the bigining of the file (original array), does objects work the same ? In the other hand, passing a String as this example

changeString(String string)
string = "string changed";

the original string will not be affected even if I called this method and printed the string

Example of object passing
student is the Object of type student

changeObjectContent(Student student){
   student.setName("New Name");
}

this will refer to the original Student object or will create a new object that will not affect the result of the original ?

Thank you
Regards

Passing an array works just like passing an object because an array is an object. For example:

/** Fails to change the string, but changes the local variable named "string"
    No Strings are copied when this method is called.
  @param string is a local variable, not an object */
public static void changeString(String string) {
    // There are two objects here: the one that string used to point to, and "string changed"
    // The object that string used to point to is discarded, not modified
    string = "string changed";
}

/** Do the same thing with an array. Change the local variable array, but leave the object
   untouched.
   @param array is another local variable that happens to point to an array
      that will not be modified */
public static void changeArray(int[] array) {
    // The same thing as before, a local variable changed, and two objects that don't change.
    // No one outside of this method will ever know that we changed this variable
    // It is for use only in this method.
    array = new int[] { 1, 2, 3 };
}

public static void main(String... args) {
    String string = "original string";
    int[] array = { 3, 2, 1 };
    changeString(string);
    changeArray(array);
    // Now prove that string and array are both untouched
    System.out.println(string); // Prints: original string
    System.out.println(Arrays.toString(array)); // Prints: [3, 2, 1]
}

It is all quite simple if you remember that doing something like x = thing causes the variable x to change, throwing away whatever it points to and replacing it with the value of thing. Throwing away what x points to does not change any object; it only changes the variable x, and x is not the same as the object that it points to.

On the other hand, if x points to an array, then you can do something like x[0] = thing, and now you are changing the array and leaving x unchanged. If that array was passed into a method as an argument, then whoever called that method will now have a changed array; no object is ever copied just because it is used as an argument. Also, if x points to an object with a field name, then doing x.name = "Bob" will change the name of the object. Also, calling a method like x.setName("Bob") may change the object, but that depends upon what setName does.

Dear bguild, what you wrote make a lot of sense, but let me try to make a conclusion of what I just understood from this post.

Whenever, in the main(), I define a string, then I call a method that has a parameter (String string)

changeString(String string){
    string = "string changed";
    //This is considered as:
    //string = new String("string changed");
}

So when I use the parameter object passed, and re-initialize it, it will be treated as seperate and none pointer to the original object. However, if in the function, we passed an object and in the body of the function we have object.name = "changed", this object will point to the original passed object and will modify its name, exactly like in arrays case where I say array[0] = "created", so I change the first element in the original array.

Lets deal with this code:

public class Student {
    public static void main(String[] args) {
        STD std = new STD();
        std.create(std);
        System.out.println(std.name);
    }
}

class STD{
    String name;

    public void create(STD std){
//      std = new STD();
        std.name = "created";
    }
}

OUTPUT:
created

Here in the function I changed the name of the original object by specifying an attribut = someThing. In case I remove the comment on the std = new STD(), this will create a seperate local variable that will die after exiting the function.

Is this conclusion right ?

Don't confuse variables and objects. std in the create method is a variable, specifically a reference variable. It's scope is the body of the create method, and it will cease to exist immediately when the method finishes executing. Line 13 does not create a new variable, it just changes the value of the existing variable.
new STD() creates an object, and std = new STD() sets the value of std to be a refeence to that object. In the absence of any other code, std is the only reference to that new object, so when std goes out of scope there will be no remaining references to the object. The object therefore become unreachable, and will be garbage collected at some unspecified later time.

aha thanks JamesCherrill, so after the garbage collects the object created in the create method, the old value of the original passed object will remain as if no changes were made to the object right?

It should probably be pointed out that string = "string changed" is not the same as string = new String("string changed"), because String(String original) is a copy constructor that creates a new string that is different from original but contains the same characters. I have never heard of that constructor being useful for any purpose, but it certainly does something.

so after the garbage collects the object created in the create method, the old value of the original passed object will remain as if no changes were made to the object right?

With line 13 executed, the variable std refers to the new object, so line 14 changes the name in the new object, which is pretty much useless as that object then gets garbage collected. Because std no longer refers to the original object, nothing you do thereafter will effect the original object.
(ps you have two vars called std in your program, everything I said refers to the one in the create method)

Okay, things are clearer now, Thanks bguild and JamesCherrill

This article has been dead for over six months. Start a new discussion instead.