3.3 Working with data
Type conversion
In Java programming, when working with values, you should carefully consider data types to avoid compilation errors. For example, if a float variable is passed to a method that requires an int value, then this will lead to a compiler error. This means that often in order to process data, it must be converted from one type to another. Numeric values can be easily converted (cast) from one numeric data type to another using the syntax:
(data-type) value
Values of a character data type (char) can be automatically used as values of an integer type (int) because they each have a unique integer representation, namely the numeric ASCII code supported by the Java compiler. For example, the Latin letter A has a numeric code of 65. Numeric values can be converted to a string type (String) using the toString () method. It takes a numeric value as an argument in its parentheses. For example, converting an integer variable num to a string type is Integer. toString (num). Similarly, the conversion of a floating-type variable num to a string will have the form Float.toString (num). In fact, such conversions are not always required, because, for example, concatenation of two variables into a string, if one of them is of a string type, occurs automatically.