-1

In Python

a,b =b,a+b

is there anything similar in Java? I tried the same thing in Java but it didn't work.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Chao Peng
  • 87
  • 2
  • 8

1 Answers1

0

No, not exactly. Multiple assignment in Python does not work the same way in Java. In Java, if you assign multiple variables at once, you must be assigning them all to the same value.

To get similar behavior in Java, you'd have to encapsulate a and b in an object or other collection, then do the assignment based on setting/returning an object of that type. (Then there is the extra step of unpacking the values of a and b from that enclosing object or collection.)

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880