I'm fairly new to Java and I'm trying to check if a variable is null and use its value if its not. Previous developer wrote something like this:
xModel.setName(xService.getName(xID) != null ? xService.getName(xID) : "");
And I would like to refactor it so I wouldn't have to use xService twice to just get the name.
I know I can store the value beforehand but this is just an example. I just wonder if there is a way to do this in Java?
Thanks.
a?b:cis an expression that will result inborcwhat you do withborcafterwards is for you to decide. You can use it in asdignments (x=a?b:c) or instantiations (x=a?new B(): new C()) or anywhere you like. But there is no actual "ternary assignment operator".