简体   繁体   中英

Understanding Java, why does this happen?

int x = 8;
int y = x;
x = 4;
   System.out.println(x + ", " + y);

Hello, I am trying to teach myself some basics of Java and I am currently looking at this example. In this, I know that the output will be: 4, 8

I however, do not know WHY that is the output, why is it that the first x comes out as a 4 and not an 8?

If I also change the int x to something else, it also makes the code incompatible. I would have thought that, as it seems to be different to the x = 4 parameter it would not matter if the int x changed?

If the int x is reliant in some way on the x = 4 line, why is the output then 4, 8 and not 8, 8? I do not know why x = 4 is having an effect on the rest of the code?

Thank you in advanced for any help in regards to this issue.

It might be easier to look at what happens line by line:

int x = 8; // Declare your variable "x" and save 8 into it.

int y = x; // Assign the value of x to the new variable y.
           //  at this point, y = 8 and x = 8. Note that the value of 
           //  x does not change.

x = 4;  // Now set the value of x to 4. y is still 8 and x is now 4.

In java, when you use equals sign, it can mean different thing depending if you are working with Objects or primitives. Object equals object means that the first object reference equals the second object reference - so if you modify one the second is modified too. But when you equals two primitives you assign second one value to the first one, so when you modify one the second remain intact.

OK, imagine you are playing football game. Till "int x = 8;" you are not in the play, you are a substitute. Then manager tells you "go in and wear yellow shirt". So here you are wearing a yellow shirt. I call yellow as "8". In the next minute, manager tells you, "give one more yellow shirt to y". You do this also. Now both you x and y are wearing yellow shirts. After sometime, manager tells you "it's enough seeing in yellow, take of it and wear black shirt". Here "black" means 4. Then you wear it.

Referee asks now: "Hey you x and y, which color are your shirts ?".

You tell black as x. y tells yellow. I mean 4, 8.

This is just a dummy example I know, but sometimes it is very helpful connecting real life examples with codes.

One another thing, java has two variable value type. Primitives and References. Please have a look at this topic. If you understand this really good then you would no doubt about this kind of code snippets.

Why shouldn't there be another output than 4,8 ? In Java there are no concepts like references what you maybe heard from C++ or so, and even then would the output be 4,4 . So what you really (only) do is:

  1. setting x to 8
  2. setting y to 8 because x has this value
  3. overwriting x to 4 and not changing anything on y , because there is absolutely no connection between them
  4. outputting x and y as they are at this point, therefore: 4,8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM