tencentyun / cos_android_sdk

MIT License
16 stars 12 forks source link

发现一个简单的错误 #7

Closed homelajiang closed 6 years ago

homelajiang commented 7 years ago

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"));

bradyxiao commented 7 years ago

? : 表达式: 3元运算符支持这样书写

homelajiang commented 7 years ago

@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"));