【发布时间】:2018-01-12 10:53:21
【问题描述】:
遇到一个问题,代码如下:
public class Test {
public static void main(String[] args) {
String str1 = "1";
String str2 = "2";
String str3 = "3";
boolean flag = true;
// way 1
test(flag? str1, str2, str3: str1);
// way 2
test(flag? (str1, str2, str3): str1);
// way 3
test(flag? new String[]{str1, str2, str3}: str1);
// way 4
test(flag? new String[]{str1, str2, str3}: new String[]{str1});
// way 5
test(flag? str1: str2);
}
private static void test(String... args) {
for(String arg: args) {
System.out.println(arg);
}
}
}
我用了五种方式调用方法test():
方式 1 调用失败。我以为我错过了括号。
方式 2 失败。我还以为是(str1, str2, str3)的问题,Java编译器没看懂。
方式 3 失败。 new String[]{} 是 String[] 对象,为什么 Java 编译器还是看不懂?
方式 4 成功。冒号的左右参数是同一类型。所以,我用方式 5 调用它。
方式 5 调用成功。
我猜的:
?(1):(2), the parameters in place 1 and 2 must be the same type?
谁能对 operator: 有很好的了解?解决我的困惑?谢谢。
【问题讨论】:
标签: java operator-keyword