As we all now, you can use ternary operators like this:
String testString = "test";
int myInt = testString.equals("test") ? 1 : 2;
However, how can I do so with methods? Assume I have the following:
String testString = "test";
if (testString.equals("test") {
doSomething();
} else {
doSomethingElse();
}
Is there something where you can combine this like the following manner:
testString.equals("test") ? doSomething() : doSomethingElse();
When using it in Android studio, it results in saying "not a statement"
Edit It can be done by initializing a local variable, but it is a bit unusual.
public void test() {
String testString = "test";
boolean test = testString.equals("test") ? doSomething() : doSomethingElse();
}
public boolean doSomething() {
return true;
}
public boolean doSomethingElse() {
return false;
}