在java中强制转换的优先级是什么? [英] What is the priority of casting in java?

查看:217
本文介绍了在java中强制转换的优先级是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一行代码类似

int s = (double) t/2   

是否与

int s = (double) (t/2)

int s = ((double) t)/2

推荐答案

这应该会让事情变得更加清晰。简单地说,一个演员优先于一个除法运算,所以它与是相同的。给出相同的输出

This should make things a bit clearer. Simply put, a cast takes precedence over a division operation, so it would be the same thing as give the same output as

int s = ((double)t) / 2;

编辑:正如knoight指出的那样,这在技术上并不相同没有括号的操作,因为它们也有优先权。但是,出于本示例的目的,它将提供相同的结果,并且用于所有意图和目的。

As knoight has pointed out, this is not technically the same operation as it would be without the parentheses, since they have a priority as well. However, for the purposes of this example, it will offer the same result, and is for all intents and purposes equivalent.

这篇关于在java中强制转换的优先级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆