What is the priority of casting in java?

前端 未结 1 1691
不思量自难忘°
不思量自难忘° 2020-12-03 03:36

if I have a line of code that goes something like

int s = (double) t/2   

Is it the same as

int s = (double) (t/2)


        
相关标签:
1条回答
  • 2020-12-03 04:01

    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;
    

    Edit: 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.

    0 讨论(0)
提交回复
热议问题