Closed homelajiang closed 6 years ago
? : 表达式: 3元运算符支持这样书写
@bradyxiao 运算符优先级比较:“+”大于“==”大于“?:” 所以原代码的执行顺序是: 1、" access_url= " + putObjectResult.access_url 2、1中的结果 (res) == null 3、2中的结果参与三元运算符
执行顺序和预期的执行顺序不一致。 例如:
String a = null;
System.out.println("res:" + a == null ? "null" : "not null");
System.out.println("res:" + (a == null ? "null" : "not null"));
https://github.com/tencentyun/cos_android_sdk/blob/master/COSDemo/src/main/java/com/example/qclouddemo/Sample/PutObjectSample.java 中,第58行
stringBuilder.append(" access_url= " + putObjectResult.access_url == null?"null":putObjectResult.access_url + "\n");
执行顺序有问题,应该加个括号的吧
stringBuilder.append(" access_url= " + (putObjectResult.access_url == null ? "null" : result.access_url + "\n"));