Maksim Dmitriev
Maksim Dmitriev

Reputation: 6209

Java copying understanding

After testing the code (see below), I found out that I don't understand some fundamentals.

Class A.

class A {

    private String s;
    private int[] array;
    private B b;

    public A(String s, int[] array, B b) {
        this.s = s;
        this.array = array;
        this.b = b;
    }
}

Class B.

class B {

    public int t;

    public B(int t) {
        this.t = t;
    }

}

I thought that any changes I did after A a = new A(s, array, b); would affect a. Don't all the fields of a and the variables s, array, b refer to the same object?

    String s = "Lorem";
    int array[] = new int[] {
            10, 20, 30
    };
    B b = new B(12);
    A a = new A(s, array, b);
    s = "Dolor";
    array = new int[] {
            23
    };
    b = new B(777); // Initialized with a new value, a.b keeps the old one. Why?
    System.out.println(a);

The output.

String Lorem
Array [10, 20, 30]
B 12

And about this.

B b2 = new B(89);
B b3 = b2;
System.out.println(b3);
b2 = null;
System.out.println(b3); // b2 initialized with null, b3 keeps the old one. Why?

The output.

89
89

However, if I have two lists, this shows that they both refer to same object.

    ArrayList<String> first = new ArrayList<String>();
    first.add("Ipsum");
    ArrayList<String> second = new ArrayList<String>();
    second = first;
    first.add("The Earth");
    System.out.println(second);  

The output.

[Ipsum, The Earth]

Upvotes: 1

Views: 128

Answers (4)

Karthi
Karthi

Reputation: 1211

To make yourself more clear try these lines of code..

String s = "Lorem";
int array[] = new int[] {10, 20, 30};
B b = new B(12);
//A a = new A(s, array, b);

s = "Dolor";
array = new int[] {23};
b = new B(777);
A a = new A(s, array, b);
System.out.println(a);


ArrayList<String> first = new ArrayList<String>();
first.add("Ipsum");
ArrayList<String> second = new ArrayList<String>();
second = first;
second.add("The Earth");
first.remove("The Earth");
System.out.println("second :"+second);

What are the current values the String s, array and the object b are holding while creating the instance of class A ( at the time of calling class A constructor ) will be printed. After creating class A instance, the String s, array and object b will be referred as a.s, a.array and so. If you assign a new value to s, array and b, it wont affect the class A instance.

And for the array list question, the two array lists will refer the same reference only. If you want different reference then do like this... (But always = assign operator will make same reference only )

    ArrayList<String> first = new ArrayList<String>();
    first.add("Ipsum");
    ArrayList<String> second = new ArrayList<String>(first);
    second.add("The Earth");
    System.out.println("first :"+first);        
    System.out.println("second :"+second);     

Upvotes: 1

mathotkadar
mathotkadar

Reputation: 1

Thanks Dukeling for explaing how assignments works with objects and primitives, I am adding here to explain how the list works when operations performed on them.

When we consider the two array lists created in the above code in the question, both the variables first and second are pointing to the same array object that resides in the memory.

So when an add operation is performed the underlying object itself gets updated. So the print operation prints the second array list which pointed to the same array-list object created during the creation of first array-list.

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55609

The difference is assignment versus modification.

Assignment (=) makes the variable point to something else, so this won't change the underlying data. So any other variables pointing to the same data don't change.

Modification (pretty much anything except =) doesn't change what the variable points to, it just modifies the underlying object. So any other variables pointing to the same data do change.

For you example:

b = new B(777); is assignment, so only b is changed to point to something else. a.b won't change.

b2 = null; is assignment, so only b2 is changed to point to something else. b3 won't change.

If you were to say b2.t = 5, this would be modification (we're not assigning a new value to b2, we're modifying it by changing one of its members), so b3 will change as well.

I hope that explains it.

Upvotes: 4

Erik Pragt
Erik Pragt

Reputation: 14637

No. The thing is, you are not changing a, you are assigning a new value to s. S is a String, which are immutable, which means you can never make a change to the value of s. You can, however, change the reference in S, which is what you are doing.

Upvotes: 1

Related Questions