简体   繁体   English

了解Java,为什么会发生这种情况?

[英]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. 您好,我正在尝试自学Java的一些基础知识,目前正在研究此示例。 In this, I know that the output will be: 4, 8 在此,我知道输出将是: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? 但是我不知道为什么会这样,为什么第一个x是4而不是8?

If I also change the int x to something else, it also makes the code incompatible. 如果我也将int x更改为其他内容,那么也会使代码不兼容。 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? 我本以为,因为似乎与x = 4参数不同,所以如果int x更改了,那没关系?

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? 如果int x以某种方式依赖x = 4行,那么为什么输出是4、8而不是8、8? I do not know why x = 4 is having an effect on the rest of the code? 我不知道为什么x = 4对其余代码有影响?

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. 在Java中,当使用等号时,这可能意味着不同的事情,具体取决于您是使用对象还是基元。 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;" 直到“ 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". 我称黄色为“ 8”。 In the next minute, manager tells you, "give one more yellow shirt to y". 在下一分钟,经理告诉您,“再给黄色衬衫给y”。 You do this also. 您也可以这样做。 Now both you x and y are wearing yellow shirts. 现在,您x和y都穿着黄色衬衫。 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. 这里的“黑色”表示4。然后就穿上它。

Referee asks now: "Hey you x and y, which color are your shirts ?". 裁判现在问:“嘿,x和y,您的衬衫是哪种颜色?”。

You tell black as x. 您将黑色告诉x。 y tells yellow. y告诉黄色。 I mean 4, 8. 我是说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. 另一件事,java有两个变量值类型。 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 ? 为什么不应该有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 . 在Java中,没有像引用这样的概念,您可能会从C ++那里听到类似的信息,即使这样,输出也将是4,4 So what you really (only) do is: 因此,您真正(仅)要做的是:

  1. setting x to 8 x设置为8
  2. setting y to 8 because x has this value y设置为8因为x具有此值
  3. overwriting x to 4 and not changing anything on y , because there is absolutely no connection between them x覆盖为4并且不改变y ,因为它们之间绝对没有连接
  4. outputting x and y as they are at this point, therefore: 4,8 此时输出xy ,因此: 4,8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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