1

In python i can do following:

return x or y

or

my_function(x or y)

if x != null or not empty will return or pass x else y.

My question:

There is way do this in java shorten then
return x == null ? y : x or

myFunction(x == null ? y : x)
Tany
  • 393
  • 1
  • 4
  • 16

2 Answers2

1

I believe the ternary method is the most efficient and shortest. But, if you want to go further and shorten things up, you could just create a generic static or non-static method, depending on your situation, to validate the two objects. Then just return the validated method, instead of the ternary condition. Something like this for example.

//usage
Employee valid(Employee x, Employee y) {
    return v(x,y);
}

//generic method for validation
<T> T v(T o1, T o2) {
    return o1==null?o2:o1;
}
Kamakazi
  • 26
  • 4
0

Ok I think what you wanted to do is passing the resulting value from

x==null ? y : x

as argument to a function.

I found this on Stack Overflow: Short IF - ELSE statement . Maybe this will help.

In short: myFunction(x==null ? y : x); should work fine.

Community
  • 1
  • 1
picatrix
  • 11
  • 5
  • yes, but my question, there is way do it shorten than myFunction(x==null ? y : x) like in python – Tany Mar 27 '16 at 18:48