0

I just thought of this question: Using Java

int x = 1
int y = x;
x = 5;

why doesn't y = 5 now?

  • 1
    Because java primitive type are immutable – Ashraful Islam Mar 29 '17 at 07:00
  • You assign a value not a reference to `x` – Jens Mar 29 '17 at 07:00
  • beacause you only set y's value to the current x value, not it's memory pointer. – abbath Mar 29 '17 at 07:01
  • Not a bad question given that Java muddies this up when it comes to non-primitives. Not half as confusing as C# though. – Bathsheba Mar 29 '17 at 07:02
  • Why would y be 5 after this code, according to you? If you can explain that, we can explain where you go wrong. – Erwin Bolwidt Mar 29 '17 at 07:02
  • 1
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Ashraful Islam Mar 29 '17 at 07:03
  • @Bathsheba Why do you bring "primitives" into the picture? The code wouldn't work with reference types either. `String x = "1"; String y = x; x = "5";` -- exactly the same problem – Erwin Bolwidt Mar 29 '17 at 07:03
  • Because in java primitive types hold only value. – Mustafa Çil Mar 29 '17 at 07:03
  • Methinks this is a "before you've studied Java" question, so in that sense is language agnostic. Note that the C++ code `int x = 1; int y& = x; x = 5;` would set the value of `y` to 5 too. Also `java.lang.String` is a kludge since it messes around with essentially numeric operators. – Bathsheba Mar 29 '17 at 07:04
  • 1
    @mustafacil In Java, reference type variables also only hold a value, the reference to the object. You cannot overload the assignment operator in Java like you can in other languages, so for the question of the OP it's irrelevant that `int` is a primitive type - exactly the same thing would have happened with a reference type. – Erwin Bolwidt Mar 29 '17 at 08:11
  • @Erwin Bolwidt reference type holds a value of course but the value is a object's adress not like '5' or '10'. I mean that, a primitive's value is different from a reference's value. – Mustafa Çil Mar 29 '17 at 08:21

4 Answers4

1

Because y is a separate variable to x, albeit initialised with the original value of x.

y is not a reference to x, or a reference to the same object as x. (int is a primitive type in Java).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1
int x = 1
int y = x;
x = 5;

primitive value is copied on this line int y=x; This is not copy of reference of an object which x is pointing to.

For reference : http://javarevisited.blogspot.hk/2015/09/difference-between-primitive-and-reference-variable-java.html Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
vlaxmi
  • 468
  • 4
  • 18
1
int x = 1; //Some memory is initialized(say at location ox00001) and x is pointing to that

int y = x ; //Some memory is initialized(say at location ox00050) and value of x is copied to that memory 

x = 5 ; //value of memory location of x (i.e. ox00001) is changed to 5 but is not impacting memory location of y 

But in case of Non-Primitive data type it shares memory location instead of data.
For reference http://javarevisited.blogspot.in/2015/09/difference-between-primitive-and-reference-variable-java.html

MishraJi
  • 304
  • 2
  • 6
  • 18
0

Take out two pieces of paper.

Write "x" on one, and "y" on the other.

Now write "1" on the paper you labelled "x". (int x = 1;)

Then take the number you see on the "x" paper and write that same number on the "y" paper. (int y = x;)

Then erase the number on the "x" paper and write "5" there instead. (x = 5;)

Observe that the number on the paper you labelled "y" did not change.
Variables work exactly like that.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82