Does "x = x = .." (double equals/assignment) mean anything special?

In a server programmed in Java, I found this statement:

buffer = buffer = new byte[bufferSize];

What I don't understand is that "buffer = " is stated twice.

Would not this code do the exact same thing?

buffer = new byte[bufferSize];

Thanks.

Upvotes: 0

Views: 75

Answers (2)

jdphenix
jdphenix

Reputation: 15445

Java assignment (=) is evaluated right-to-left so,

  • First buffer is assigned to new byte[bufferSize]
  • then buffer is assigned to the value of buffer

While the second statement is exactly as you'd expect. buffer is assigned to new byte[bufferSize].

It's likely the compiler would optimize this away.

Upvotes: 3

ortis
ortis

Reputation: 2223

No difference between the two. It's a typo.

Upvotes: 1

Related Questions