返回信息流public class HelloWorld {
public static void main(String []args) {
int a=0;
a>0?funA():funB();
}
public static void funA(){
System.out.println("A!");
}
public static void funB(){
System.out.println("B!");
}
}
a>0?funA():funB();这条语句是错的
boolean表达式 ? 表达式1 : 表达式2
我理解的这个三元运算符只能用于赋值或者return,也就是说表达式1和表达式2必须是有返回值的。但是我也不知道自己理解的对不对,也说服不了我同事。所以各位大佬有没有官方文档或者比较正规的解释来说服一下我同事?
感谢!!!
这是一条镜像帖。来源:北邮人论坛 / java / #64800同步于 2020/12/3
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
三元运算符后面的表达式
wxn0927
2020/12/3镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
【 在 a2682484253 的大作中提到: 】
: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
: It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
太感谢啦!!!