Detailed explanation of Java data overflow code

Java is a relatively safe language, so how does it handle data overflow? Look at a piece of code,

The result of the run is:

The int type is four bytes in Java and is divided into positive and negative, so the maximum int value is 0x7fffffff. The variable bigger obviously overflows, but you don't get compilation errors or runtime errors. It looks good, but sometimes it's not necessarily a good thing. Like this example, the expected result should not be - 4, and the program has no error prompt. So how to solve the problem of overflow? It is easy to think of a scheme that uses a larger data type. The long type takes up 8 bytes and can be used,

Output,

Continue to think about a question. Why is the result of overflow in the first example - 4? We can get some enlightenment from the results of the second example. The hex of 8589934588 is 1ffffffc, leaving only four bytes (int type only accounts for four bytes) as fffffc. This is a complement, and because the highest bit is 1, it is still a negative number. Convert to the original code (except for the sign bit, take the inverse plus 1) to 10000004, that is - 4.

The following example is seen from another article and will be shared with you.

Output results:

It's a little strange. The calculated value of 1000 * 60 * 60 * 24 * 30 * 3 is different from that of 1000L * 60 * 60 * 24 * 30 * 3. The calculated result of 1000 * 60 * 60 * 24 * 30 * 3 is - 813934592

And (long) 1000 * 60 * 60 * 24 * 30 * 3; 1000L*60*60*24*30*3; The calculation results are 7776000000, which is what we need

Why does this happen? When Java multiplies, if the determined data type is not displayed, it will calculate according to the int data type by default, just like 1000 * 60 * 60 * 24 * 30 * 3. This value causes int data overflow during calculation, so there is a negative number.

(long)1000*60*60*24*30*3; 1000L*60*60*24*30*3; For the two, the former forces the first calculation number to be long, and then obtains the correct calculation result according to the long type operation. The latter can directly obtain the correct calculation result by modifying the data type to long during calculation.

This point is still very obscure. We should pay attention to prevent mistakes in the future. This error is hard to check.

Another point is the location of L, which is a little strange. 1000L * 60 * 60 * 24 * 30 * 3; 1000*60L*60*24*30*3; 1000*60*60L*24*30*3; 1000*60*60*24*30L*3; These writing methods can be correctly converted to long type, but 1000 * 60 * 60 * 24 * 30 * 3L; It's not right, and it's OK for L to follow any number when writing 3 * 60 * 60 * 24 * 30 * 1000. It's a little puzzling. When using the debug test, you should first determine the calculation result of this string.

summary

The above is all about the detailed explanation of Java data overflow code in this paper. I hope it will be helpful to you. Welcome to browse other topics of this site. If you have any questions, please leave a message. Xiaobian will reply to you in time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>