I am looking for a more efficient way to perform the following action, where I am appending the return value of a method to an array. However if the return is null i want to perform another action and not change the array.
Object[] myVal = new Object[10];
Object temp;
temp = myFunction();
if(temp != null) {
myVal[count++] = temp;
} else {
System.Exit();
}
Where I want to assign a variable to the return of a method, but provide an 'inline' check to carry out another action if the value returned is null.
Something like:
myVal = (myFunction() != null) ? [output of expression]: System.Exit();
Is there any method like this?
myVal = myFunction();
if (myVal == null) System.exit(0);
No need for a temp variable.
Try this:
if ((myVal = myFunction()) == null) System.exit(0);
else // do your work
Note: Usage of ternary operators is not recommended because code becomes hard to read.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.