jslvtr jslvtr - 4 months ago 7
Java Question

Assign variable value inside if-statement

I was wondering whether it is possible to assign a variable a value inside a conditional operator like so:

if((int v = someMethod()) != 0) return v;


Is there some way to do this in Java? Because I know it's possible in
while
conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.

Answer

do something like

int v;
if((v = someMethod()) != 0) return true;