Let's talk about off-peak computing ~! Is your computing method overflow ?, Those things
The game is almost officially launched. Today, a bug is found, making people laugh and cry. Data computing overflows; the player's recharge ingot is changed to 0; this is a big event, and no one can afford this responsibility after all;
Let's talk about the reason. The development language is java. The tool is netbeans ide 8.0.2.
A player object has an attribute of the int type, which is gold;
The calculation method for player recharge is as follows.
Int gold = 20000; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = gold + tempGold) {gold = gold + tempGold;} else {gold = Integer. MAX_VALUE ;}
It seems that there is no problem, right. Of course the above is simulated;
If you have a wealth of experience, or see some clues below, it will be overdone;
Maybe you don't see any problems. I didn't find any problems at the beginning, so the code is written like this. Well, let's simulate it.
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = gold + tempGold) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2 ");}
What is the output ????
Maybe you will answer and Output 2 pairs. Yes, I thought it would output 2,
But why is the running result 1?
First, let's analyze why we thought we would output 2? Obviously
Gold + tempGold
The calculation of these two values is assumed to be long and greater than Integer. MAX_VALUE.
However, this is not the case. Let's take a look at the output results.
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = gold + tempGold) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2");} System. out. println (gold + tempGold );
--- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---1-2147445449------------------------------------------------------------------------BUILD SUCCESS
Under the java mechanism, the addition of gold + tempGold is not a long type, but a negative number.
You may laugh at me. Okay, I admit, I did not verify the problem. Fortunately, the game has not yet been launched. Problems found during testing.
However, I have discovered this problem and learned the problem. All right.
That is, the problem of converting int to long.
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // the player's current top-up long tempLGold = tempGold; if (Integer. MAX_VALUE> = gold + tempLGold) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2");} System. out. println (gold + tempGold );
Test the current output result?
--- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---2-2147463649------------------------------------------------------------------------BUILD SUCCESS
These are correct, okay .. The second offense is over. However, it is found that there is a variable long tempLGold; the attributes and operations are not very convenient, and there are no better operations;
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = gold + tempGold + 0L) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2 ");}
Pay attention to the following 0L
View output
--- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---1------------------------------------------------------------------------BUILD SUCCESS
The result is still Output 1. Maybe you will laugh at me for having FAN 2, but it is true that I have done something wrong. I will only know the operator priority in depth.
Well, change it.
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = 0L + gold + tempGold) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2 ");}
Output
--- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---2------------------------------------------------------------------------BUILD SUCCESS
Yes
Int gold = Integer. MAX_VALUE-1800; // the player's original int tempGold = 20000; // if (Integer) of the player's current recharge. MAX_VALUE> = gold + tempGold * 1L) {gold = gold + tempGold; System. out. println ("1");} else {gold = Integer. MAX_VALUE; System. out. println ("2 ");}
--- exec-maven-plugin:1.2.1:exec (default-cli) @ game-gamesr ---2------------------------------------------------------------------------BUILD SUCCESS
This is correct ,,,
The second thing a programmer can do...