Java two equal signs in one statement? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-04 03:30:00

问题


Can someone help me understand what the following code does and what the line with two equal sign does? How does something equal to something equal to something work in this constructor?

public More ...LinkedList() {
      header.next = header.previous = header;
 }

Here is the link to the website where I saw this and I'm trying to figure it out: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#LinkedList.0header


回答1:


Read assignment statement from right to left:

  1. assign header to header.pevious
  2. assign header.previous to header.next

The bottom line: after this line both header.previous header.next will refer to header.




回答2:


header.next and header.previous have same value of header.

Example:

int val1 = 10;
int val2 = 11;
int val3 = val2 = val1;

Here At last val1,val2 & val3 has the same value as 10




回答3:


A single = is the assignment operator. This is a way to do multiple assignment in one line of code. It is setting header.next and header.previous to the value of header.

  1. header.next = header.previous = header;

Is the same as...

  1. header.next = header;
  2. header.previous = header;



回答4:


This means both header.next and header.previous will be set to header.




回答5:


Its as simple and as similar as a = b = 10 were value 10 is assign to variable b (b=10) and then value of variable b is assign to variable 10 (therefore a = 10). Please see here for more details.



来源:https://stackoverflow.com/questions/21661251/java-two-equal-signs-in-one-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!