Type-promotion issues in expressions
int a = 3;
Long B = 4;
Double d = 5.3;
int c = (int) (A+B); Do all the two-dollar operators (+-*/%), there will be type promotion problem!
float f = (float) (a+b); A is int,b to Double,a+b will be promoted to double type, and when assigned to F, F is float type, the stock needs to be cast!
Error-prone exercises:
Earn 1 billion a year, 20 years to make how much money?
int money = 1000000000;
int years = 20;
Long total = (long) money*years; This ingenious use of the forced transformation of the type of promotion, solved the problem beyond the scope of int
SYSTEM.OUT.PRINTLN (total);
How many times a person's heart beats in 70?
Long times = 70l*60*24*365*70; Ditto, but cast to L, as far as possible after the first 70, because in some cases, if you put L after the last 70, you can not determine whether the previous value multiplication is out of range, the preceding multiplication has gone beyond the scope, and finally forced conversion type promotion, it will not help.
System.out.println (times);
Java Learning notes-problem with type promotion