10

Say I set int A = int B. When I change A after, it will not change the value of B. But when I set a SomeClass A = SomeClass B, and I change A's contents (like a.cost), it changes B.cost as well. Can someone explain this to me?

I thought Java is assigned by value, not reference?

Brent
  • 101
  • 1
  • 1
  • 3

5 Answers5

31

Yes, it does - but the value of A is a reference, not a copy of the object itself.

I like to give the following analogy...

Suppose two people both have my address: that's like two variables of type House in Java. Now one of them comes and paints my door red. The second person will still see the red door if they visit:

House jonsHouse = new House(); // Even the variable jonsHouse is only a reference

House firstAddressCopy = jonsHouse; // Just a copy of the reference
House secondAddressCopy = jonsHouse; // Just a copy of the reference

firstAddressCopy.paintDoor(Color.Red);

Color color = secondAddressCopy.getDoorColor(); // Now color will be red

Basically, remember a few rules and things will become clear:

  • The value of an expression in Java is never an object - only ever a reference or a primitive value
  • (Corollary of first point) A variable never holds an object - only ever a reference or a primitive value
  • Assignment (and argument passing) always copies the value, whether that value is a reference or a primitive value
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

I thought Java is assigned by value, not reference?

What does "assigned by value" mean? Are you maybe confusing it with "pass by value/reference"?

At any rate, if you handle a class instance in Java, you are actually handling a reference to that class (much like a pointer in C/C++). Your assignment only copies the reference, so both A and B refer to the same instance, i.e. the data is shared, hence the result.

sleske
  • 81,358
  • 34
  • 189
  • 227
1

A is a reference to the object. So if you change the object internal state, it will be reflected to every other variable pointing to it.

If you re-assign A, then B will not change:

Foo a = new Foo();
Foo b = a;
a.bar = "bar"; // this is reflected in b
a = new Foo(); // b stays pointing to the previous Foo
a.bar = "baaar"; // b stays with a value of bar="bar"

(Java is pass by value. Check this article about it.)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Java is strictly pass by value. When passing a reference by value only the reference is duplicated, not the object being referenced. – Russell Zornes Oct 04 '10 at 22:03
  • @Maven, of course, a stupid mistake, I thought one and wrote the other :) thanks – Bozho Oct 04 '10 at 22:07
0

In Java, your variables can be split into two categories: Objects, and everything else (int, long, byte, etc).

A primitive type (int, long, etc), holds whatever value you assign it. An object variable, by contrast, holds a reference to an object somewhere. So if you assign one object variable to another, you have copied the reference, both A and B point to the same object.

NOTE: Strings in Java are actually objects, not primitives, which beginners often assume.

Hope this helps

will
  • 1
  • 1
0

In java when you assign Object to Object its assign by reference. Int is non an Object in java so when you assign int to int its assign by value.

tn2000
  • 642
  • 1
  • 8
  • 13