简体   繁体   中英

Can an integer be added with a long?

I'm sorry for such a lame-o question. 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.

Is it possible to add a long and an integer together? 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. (He wants numbers to be higher than 2.147 billion).

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." 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 .

The int undergoes a widening primitive conversion, as described in the Java Language Specification, specifically JLS8, §5.1.2 .

JLS8 §5.6.2 is the important part that details what happens here (my bolding):

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

Yes you can add a long and an integer together. in java there are several primitive data types. int and long variables are two of them. these two data types are used to store integer values. the difference is the size of the variable,

int : 32bit

long : 64bit

you can add int and long together, when jvm add these two variables, the result is generated as a long value. so you have to use a long variable to store the answer. This is due to the auto type conversion of java.

if you want to get int value as an answer, you have to cast the long value in to int ,

    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

You can always simply try such things. For instance using jdoodle

As you can see it is perfectly possible. It is advisable to use a long to store the result, but not necessary. In case of overflow in the latter case, Java will simply use the 32 least significant bits, thus:

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

will be stored as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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