I have two variables A
and B
, I need to set the values of both variables in one statement using a ternary operator.
eg :
"XYZ".equals(String 1) ? (A = String A) : ("ABC".equals(String 2) ? (B = String B) : (A = B = null)) ;
is this possible?
答案 0 :(得分:4)
Yes, you can, but you should assign the result of the expression to some variable:
String value = "XYZ".equals("something") ? (A = "aValue") : ("ABC".equals("somethingElse") ? (B = "bValue") : (A = B = null));
It doesn't look very readable though.