(int) case是如何工作的?

huangapple go评论76阅读模式
英文:

How does the (int) case works?

问题

我一直在寻找一种将一种数据类型转换为另一种的解决方案(例如:double转int,或long转int等)。我找到了类似于intValue()的东西,它对我没有真正帮助,然后我找到了这个结构:

int result = (int) /这里放double值/;

我无法找到关于这个解决方案以及它是如何工作的,以及它是否适用于所有基本数据类型的任何信息。此外,如果您能解释一下intValue()的情况,我会非常感谢。我猜它在基本数据类型中不起作用,但是无论如何,我的IDE都不认识这个方法。
如果我的问题在任何教程或文章中得到了回答,请随意将我重定向。

提前感谢!和平与爱!!!

英文:

I'd been searching for a solution how to convert one data type to another( ex: double to int, or long to int & etc). I found something like intValue() which did not really help me and then I found this construction :

int result= (int) /double value here/;

I could not find anything about this solution and how it really works and if it works for all primitive data types. Also, I would really appreciate it if you could explain the case with intValue(). I suppose it doesn't work with primitive data types but my IDE does not recognise this method in any case.
If my question is answered in any tutorials or articles feel free to redirect me.

Thank you in advance!
Peace and Love!!!

答案1

得分: 1

是的。这样的强制类型转换适用于任何兼容的基本数据类型,并且会进行适当的四舍五入/截断。这是在基本数据类型之间进行转换的正常方式。

它们的工作精确规则在这里进行了定义(链接:https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.2)和在这里(链接:https://docs.oracle.com/javase/specs/se10/html/jls-5.html#jls-5.1.3)。

英文:

Yes. Casting like this works for any compatible primitive types, and does the appropriate rounding/truncation. This is the normal way to convert between primitive types.

The precise rules of how they work are defined here and here.

huangapple
  • 本文由 发表于 2020年9月10日 05:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63819548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定