Source: http://www.imooc.com/code/1236
In Java programs, it is often necessary to convert data between different basic data types. For example:
, the int variable score1 in the code can do the assignment directly for the double variable Score2, and the result is: This conversion is called an automatic type conversion .
Of course automatic type conversions are required to meet specific conditions :
1. The target type can be compatible with the source type, such as type double compatible int, but char type cannot be compatible with int
2. The target type is greater than the source type, such as the double type is 8 bytes long, the int type is 4 bytes, so the double type of the variable can directly hold the int type of data, but the reverse is not possible
Task
Little friends, let's feel the automatic type conversion.
In the editor, the code function is: Define three variables, respectively, to save: Test average, growth value, adjusted average score
The expected run result is:
Now that there is an error in line 5 , can you find and correct it?
1 Public classhelloworld{2 Public Static voidMain (string[] args) {3 Doubleavg1=78.5;4 intRise=5;5 intavg2=avg1+Rise;6System.out.println ("Average test score:" +avg1);7SYSTEM.OUT.PRINTLN ("Adjusted average score:" +avg2);8 }9}
1 Public classhelloworld{2 Public Static voidMain (string[] args) {3 Doubleavg1=78.5;4 intRise=5;5 Doubleavg2=avg1+Rise;6System.out.println ("Average test score:" +avg1);7SYSTEM.OUT.PRINTLN ("Adjusted average score:" +avg2);8 }9}
Web-android Engineer first form automatic type conversion in Java -2-9