Ternary operator to set values into multiple variables

时间:2017-12-17 08:21:03

标签: java ternary-operator

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?

1 个答案:

答案 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.