为什么在这个例子中 (int) 周围有括号? [英] Why are there parentheses around (int) in this example?

查看:41
本文介绍了为什么在这个例子中 (int) 周围有括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

decimal d = 2;
int i = (int) d;

我已经多次看到这种情况,其中括号围绕数据类型.

I've seen this several times in which parentheses are wrapped around data types.

为什么不直接使用 int i = int d;?

推荐答案

(int) 的用法叫做 casting(或者,类型转换).它本质上是在说明,解释 转换 d as 的值>int(整数)并将其存储到 i 中.

The usage of (int) is called casting (or, type-casting). It is essentially telling that, interpret convert the value of d as to an int (integer) and store it into i.

换句话说,它是一种将类型转换为另一种类型的方式(取决于转换的有效性).

In other words, it is a way of converting a type to another one (subject to validity of the conversion).

顺便说一句,int i = int d;,按原样,不是有效的语句.

BTW, int i = int d;, as is, is not a valid statement.

这篇关于为什么在这个例子中 (int) 周围有括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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