5

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

Hell Man
  • 833
  • 3
  • 18
  • 24

5 Answers5

12

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
5

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;
jeremyjjbrown
  • 7,772
  • 5
  • 43
  • 55
5

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
3

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

Warlord
  • 2,798
  • 16
  • 21
3

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.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74