问题
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:
- assign
headertoheader.pevious - assign
header.previoustoheader.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.
- header.next = header.previous = header;
Is the same as...
- header.next = header;
- 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