Conversion of two data types in Java

how data types in Java are converted

data type casting is the process of changing a value from one type to another. For example, String data 456 can be converted to a numeric type, and any type of data can be converted to String.

if you convert from a low-precision data type to a high-precision data type, you will never overflow and will always succeed. However, the conversion of high-precision data type to low-precision data type will inevitably result in information loss and possibly failure.

data type conversion can be done in two ways, implicit and explicit.

implicit cast:

the first type is implicit conversion, which is the conversion from a low-level type to a high-level type without any operation, the system will perform automatically. This type conversion is called implicit conversion. The following basic data types involve data conversions, which do not include logical types or character types. These types are classified in order of accuracy from low to high:
byte< short< int< long< float< Double
example:
int x = 100;
float y = x; The output y value is 100.0

implicit casting requires certain rules to resolve when to convert one type of data to another.
the general rule of implicit type conversion below:

shows type conversion:

the second type is display type conversion, also known as display type conversion to cast. Display type conversion must be used when assigning the value of a high-precision variable to a low-precision variable.
syntax :

may cause accuracy to disappear when performing display type conversions

( type name) value to be converted

example:
int a = (int)45.23;
int a = (int)45.23;
long b = (long)45.6f;
int c = (int) ‘d’;
output result a=45;
b = 45;
c = 100; Note: when assigning integers to a byte short int long type of variable, the values of these types must not be exceeded, or you must enter a cast. For example:
byte a = (byte)129;

reference: what are the two types of data conversion in Java?


Read More: