Mohamed Taher Alrefaie
Mohamed Taher Alrefaie

Reputation: 16253

When you make a reference = another reference and both values are null?

public static void main(String[] args) {
        ArrayList a=null, b=null;
        a=b;

        a=new ArrayList();
        System.out.println(a+""+b);
    }

Why in the world b is printed as null ?

I thought java makes references the same then whatever you change in one of them reflects the other. But not in this case !!!

Upvotes: 0

Views: 96

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1502616

This line:

a = b;

Sets the value of a to the current value of b. That's all it does. The current value of b is null, so it's equivalent to:

a = null;

It does not associate the two variables. It just copies the value of one to another.

Changing the value of a afterwards does not change b at all. The two variables are entirely separate. Note that this is exactly the same for primitive types:

int a = 10;
int b = a;
a = 5;
System.out.println(b); // Prints 10, not 5

Even if you had:

ArrayList<String> a = new ArrayList<String>();
ArragList<String> b = a;
a.add("Hello");
System.out.println(b.get(0)); // Prints "Hello"

That's still not really showing a relationship between the variables a and b. They have the same value, so they refer to the same object (the ArrayList itself) - changes to that object can be observed via either variable. But changing the value of each variable to refer to a different list (or null) won't affect either the other variable or the object itself.

One thing which may be confusing you is what the value of a or b actually is. The value of a variable (or any other expression) in Java is never an object - it's always either a reference or a primitive value.

So an assignment operator, or passing an argument to a method, or anything like that will never copy the object - it will only ever copy the value of the expression (a reference or a primitive value).

Once you understand this, Java starts to make a lot more sense...

Upvotes: 6

Marcus
Marcus

Reputation: 12596

Simply put. The variables refers to the object the other variable refers to in the moment it is set and not to the variable itself.

ArrayList a=null, b=null; // Both *a* and *b* refers to null
a=b;                      // Set *a* to refer to what *b* refers to (in this case null)
a=new ArrayList();        // Set *a* to refer to a new arraylist. *b* still refers to null.

Upvotes: 0

dnuttle
dnuttle

Reputation: 3830

Because you've redefined a. When you say:

a=new ArrayList();

You break the existing relationship between a and b.

Upvotes: 0

Daniel Earwicker
Daniel Earwicker

Reputation: 116714

Variables like your a and b are called references. They refer to objects. The objects are floating around somewhere else (they are not stored "inside" the variables). When you say a=b you make a refer to whatever b refers to. In your case that makes no difference, because both already refer to null (i.e. to no object at all).

When you assign a new object to a that makes no difference to what b refers to.

Upvotes: 2

Related Questions