Java-forced conversion

Java-forced conversion

Calculation between int types

int number1 = 13;
int number2 = 4;

System.out.println(number1/number2);  //Division between int types

//console
3

Calculation between int types "13/4" becomes 3 What if I want to get an accurate value by dividing int type variables ??

cast

int number1 = 13;
int number2 = 4;

System.out.println((double)number1/number2);

//console
3.25  //Double type calculation result

In such a case, forcibly perform type conversion This is called a cast and is the value (the data type you want to convert).

When you want to finally obtain the double type calculation result from the values โ€‹โ€‹of int types Cast either one (Java will decide the other)

Recommended Posts

Java-forced conversion
Java type conversion
Ruby text conversion
java.util.Date โ‡” java.time.LocalDateTime conversion
Hide predictive conversion
Java-automatic type conversion