[Java] Java study memo 2 with Progate

1 minute read

Type story

It seems that there are quite a lot of types of data.
I thought about int and numbers, but to be exact, they are “integers”.
Numbers with a decimal point are classified as double type.
Calculations between int types return results as int types, and calculations between double types return as double types.

System.out.println(5/2);            2    //int type
System.out.println(5.0/2.0);        2.5  //double type

Note: hand_splayed_tone2:

Since it is not possible to process data types with different data types with + etc., it is necessary to convert ** types **.

Type conversion

Automatic type conversion

//String type + int type
System.out.println("today" + 6 + "It's the moon")
=>("today" + "6" + "It's the moon")
int type is automatically converted to String type

int type and double type ⇒ double type

Because int type is automatically converted to double type.

Forced conversion (cast)

If you want to get an accurate answer (including after the decimal point) in the calculation of integers, perform forced type conversion.
For example, when you want to perform 10/4 calculation conversion → Both are int type, so either value should be double type.
Then, automatic type conversion is applied, both become double type, and the obtained value also becomes double type.
If there is no type conversion, the decimal point will be cut to 2.

Change 10 from int type to double type