jslvtr jslvtr - 1 year ago 75
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
conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.

Answer Source

do something like

int v;
if((v = someMethod()) != 0) return true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download