简体   繁体   English

整数可以加长吗?

[英]Can an integer be added with a long?

I'm sorry for such a lame-o question. 对于这个such脚的问题,我感到抱歉。 I would test this myself... But unfortunately I do not know how to code for java, and it would not be worth answering just for this one question. 我自己测试一下...但是不幸的是,我不知道如何为Java编写代码,仅就这个问题就不值得回答了。

Is it possible to add a long and an integer together? 是否可以将long和一个整数相加? My friend is working on a project, and I think he can fix one of his errors by using a long instead of an integer. 我的朋友正在从事一个项目,我认为他可以通过使用long而不是整数来解决他的一个错误。 (He wants numbers to be higher than 2.147 billion). (他希望数字能超过21.47亿)。

I tried doing a bit of research on my own, and I was surprised that the answer wasn't as easy to find. 我尝试自己进行一些研究,但令我惊讶的是,答案并不那么容易找到。 This is one source of information that I was able to find. 这是我能够找到的信息来源之一。

"If either or both of the integer types is a long, the result is a long." “如果两个整数类型中的一个或两个都为long,则结果为long。” https://community.oracle.com/message/5270213 https://community.oracle.com/message/5270213

Is that correct? 那是对的吗? Again, sorry that I'm not able to test this out myself. 再一次,对不起我无法自己进行测试。

Yes, you can add a long and an int just fine, and you'll end up with a long . 是的,您可以添加long和一个int就可以了,最后得到long

The int undergoes a widening primitive conversion, as described in the Java Language Specification, specifically JLS8, §5.1.2 . 如Java语言规范(特别是JLS8, §5.1.2所述,对int进行了扩展的原语转换。

JLS8 §5.6.2 is the important part that details what happens here (my bolding): JLS8 §5.6.2是重要的部分,详细描述了这里发生的事情(我的粗体显示):

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules: 扩展原语转换(第5.1.2节)适用于转换以下规则指定的一个或两个操作数:

If either operand is of type double, the other is converted to double. 如果一个操作数的类型为double,则另一个将转换为double。

Otherwise, if either operand is of type float, the other is converted to float. 否则,如果其中一个操作数的类型为float,则另一个将转换为float。

Otherwise, if either operand is of type long, the other is converted to long. 否则,如果其中一个操作数的类型为long,则另一个将转换为long。

Otherwise, both operands are converted to type int. 否则,两个操作数都将转换为int类型。

Yes you can add a long and an integer together. 是的,您可以将long和一个整数加在一起。 in java there are several primitive data types. 在Java中,有几种原始数据类型。 int and long variables are two of them. intlong变量是其中两个。 these two data types are used to store integer values. 这两种数据类型用于存储整数值。 the difference is the size of the variable, 不同之处在于变量的大小,

int : 32bit int :32位

long : 64bit long :64位

you can add int and long together, when jvm add these two variables, the result is generated as a long value. 您可以将int和long一起添加,当jvm将这两个变量相加时,结果将生成为long值。 so you have to use a long variable to store the answer. 因此您必须使用一个长变量来存储答案。 This is due to the auto type conversion of java. 这是由于java的自动类型转换。

if you want to get int value as an answer, you have to cast the long value in to int , 如果要获取int值作为答案,则必须cast long值intint

    int x=5;  //int value
    long y = 10;  //long value
    long z = x + y;  //result is a long value(normally jvm does)
    int i=(int) (x+y);  // result is cast into a int value.

both z and i get value: 15 z和我都得到值:15

You can always simply try such things. 您总是可以简单地尝试这些事情。 For instance using jdoodle 例如使用jdoodle

As you can see it is perfectly possible. 如您所见,这完全有可能。 It is advisable to use a long to store the result, but not necessary. 建议使用long来存储结果,但这不是必需的。 In case of overflow in the latter case, Java will simply use the 32 least significant bits, thus: 在后一种情况下,如果发生溢出,Java将仅使用32个最低有效位,因此:

6666555555555544444444443333333333222222222211111111110000000000 (index)
3210987654321098765432109876543210987654321098765432109876543210
----------------------------------------------------------------
0101010101010010000011110010101111101010101111101011101011011101 (value)

will be stored as: 将存储为:

33222222222211111111110000000000 (index)
10987654321098765432109876543210
--------------------------------
11101010101111101011101011011101 (value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM