I got this noted in a middle of a development.
Why is the Ternary operator not working inside a method argument? Here it is clearly InputStream
or (else) String
.
class A{
public static boolean opAlpha(InputStream inputStream) {
// Do something
return true;
}
public static boolean opAlpha(String arg) {
// Do something else
return true;
}
public static void main(String[] args) throws Exception {
boolean useIsr = true;
InputStream inputStream = null;
String arg = null;
// boolean isTrue = useIsr ? A.opAlpha(inputStream): A.opAlpha(arg); // This is OK.
boolean isTrue = A.opAlpha(useIsr ? inputStream : arg); // This is not. (Error : The method opAlpha(InputStream) in the type A is not applicable for the arguments (Object))
}
}
The expression
useIsr ? inputStream : arg
is of typeObject
, since that's the common type ofinputStream
(InputStream
) andarg
(String
).You don't have any
opAlpha
method that accepts anObject
. Hence the compilation error.