将int转换为long [英] Casting an int into a long

查看:135
本文介绍了将int转换为long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在java中从int转换为long的问题。
为什么浮标没有问题:

I have a question regarding the conversion from int into long in java. Why for floats there is no problem:

float f = (float)45.45;//compiles no issue.
float f = 45.45f; //compiles no issue.

然而对于长型而言似乎是一个问题:

However for the long type it seems to be a problem:

    long l = (long)12213213213;
    //with L or l at the end it will work though. 
    long l = (long)12213213213L;

似乎一旦编译器因超出范围的问题而通知错误,它会阻止没有检查程序员可能计划的任何可能的投射。

It seems that once the compiler notify an error due to an out-of-range issue it blocks there without checking for any possible casting that the programmer might have planned.

你对它有什么看法?为什么有任何特殊原因?

What's your take on it? Why is it like that there is any particular reason?

提前致谢。

推荐答案

Java不会考虑你只使用它的值来做什么。例如,如果你有一个太大的值,那么将它分配给double是没关系的,那么long值会先被检查为有效。

Java doesn't consider what you do with a value only what it is. For example if you have a long value which is too large it doesn't matter that you assign it to a double, the long value is checked as valid first.

这篇关于将int转换为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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