1

While digging in ArrayList code I spotted this code fragment

  if ((lst = list) != null && (a = lst.elementData) != null) { ... } 

Now I really don't know what this expression is called, talking about (lst = list) != null, and why it is used like this. It every assignment in java returns an assigned object or what?

Adelin
  • 18,144
  • 26
  • 115
  • 175

1 Answers1

1

You are basically assigning the value of list in lst and then doing other checks. The above code basically means

    lst = list;
    if ((lst != null && (a = lst.elementData) != null) { ... } 

why it is used like this

Probably just to save a line of code. It definitely reduces the readability a little bit.

Side note : This approach is very useful when you are doing something inside a loop.

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

Here in the while loop the value of line keeps getting updated after each iteration of while loop.

Abhishek Garg
  • 2,158
  • 1
  • 16
  • 30