0
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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    There is no "first `x`". There is just one `x`, and the code executes from top to bottom (in this case, since you have no loops). When you assigned `y = x`, at that point in time, the value of `x` was 4, so the value of `y` was set to 4. Then you assigned that same `x` the value of 8 before you did your print. So you see 8 for `x`. The symbols `x` and `y` represent distinct, singular memory locations. – lurker Oct 01 '15 at 20:53
  • So... You set `x` to 4, and then you're surprised that `x` is set to 4? It's not really clear what you're asking or why the behavior of this code is surprising to you. When you set the value of a variable and then print that variable, it's going to print the value that you set. Why... wouldn't it? – David Oct 01 '15 at 20:57
  • BTW, this isn't 'basics of java', this is basics of programming. There are a few languages where assigned values are immutable (erlang, for example) but they are few and far between. – KevinDTimm Oct 01 '15 at 20:58
  • I found it useful when learning how to program to draw little boxes for my primitive variables (i.e., *int x* ◰) and walk through the code line by line, updating the value of my boxes using a pencil and eraser as I stepped through the code. – Rainbolt Oct 01 '15 at 20:59
  • Might want to read about [Variables](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) – Andreas Oct 01 '15 at 21:03
  • Thanks to everyone here. To answer David and KevinDTimm a bit more, my issue was that I did not recognise "x" as being the same, I imagined that one being specified as "int x" changed the identification of it, so that the second "x" I imagined was a new identifier in its own right, not that they were linked or how they would interact with y. I was also not aware that the placement of the code was a factor in how it would process. And yes I am quite basic with programming and understanding which is why I'm asking for help =/... – Mystic Melody Oct 01 '15 at 21:24

4 Answers4

6

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.
dxdy
  • 540
  • 2
  • 13
  • So the reason for this is just the ordering of the code? If this was in a different order it would the process different? Can you not specify another piece of code later on to process within this earlier segment? – Mystic Melody Oct 01 '15 at 20:58
  • 1
    I think your confusion stems from the use of `=`. In Java, `y=x` means that the value of `y` will be overwritten with the value of `x`. `x` will however *not* change. If your code was in a different order, it could very well be different. For example, `int x = 8; x = 4; int y = x;` would leave x at four and y at four. – dxdy Oct 01 '15 at 20:59
  • Yes, it's the essence of [Imperative programming](https://en.wikipedia.org/wiki/Imperative_programming). You declare "statements" list of action to do for processing. Whereas [Declarative programming](https://en.wikipedia.org/wiki/Declarative_programming) (HTML, SQL, Prolog and some functional programming languages) you focus on your intention. – LoganMzz Oct 01 '15 at 21:02
  • Thank you both for these answers, this is pretty much what I was having issues with understanding but I get it now. :) – Mystic Melody Oct 01 '15 at 21:19
1

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.

  • This is a really helpful post, thank you, I was having trouble understanding how you would get a value to stick if it was dependent on another value which might be subject to change – Mystic Melody Oct 01 '15 at 21:21
0

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.

Afsin Buyuksarac
  • 298
  • 1
  • 10
-1

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
SkryptX
  • 813
  • 1
  • 9
  • 24
  • [Reference types](https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html) exist in Java, but reference types are [passed by value](http://stackoverflow.com/a/40523/3224483). – Rainbolt Oct 01 '15 at 21:07
  • Well yeah in that sense my answer is maybe a bit misleading. With reference types you actually could get a `4,4` result but not with primitive types like `int` – SkryptX Oct 01 '15 at 21:21