System.out.println("Mr. Naito"+26+"I'm old")
//console
Naito is 26 years old
Operations such as "+" must be of the same data type When operating on different types, convert the types to the same type
In Java, this type conversion method includes automatic conversion and manual conversion. If you add String type and int type The int type is automatically converted to the String type and the strings are combined.
System.out.println(5/2); //Calculation between int types
System.out.println(5.0/2.0); //Calculation between double types
//console
2 int type
2.5 double type
Precautions for numerical calculation Calculation between int types will result in int type Calculation between double types will result in double type
Dividing 5 of int type by 2 of int type Note that the result is "2" instead of "2.5"
System.out.println(5.0/2);
5.0/2.0 //Convert to double type
//console
2.5 double type
Int and double type calculations result in double type Because int type is converted to double type in the process of calculation Therefore, the result of "5 / 2.0" or "5.0/2" is 2.5.
Because integers can always be represented as decimals, like 5 to 5.0 Java automatically performs type conversion
Recommended Posts