Implicit type conversion

The Java language is strongly typed. Once a variable’s data type is specified, it remains that data type. At the same time, The Data types of the operands involved in the assignment operation and arithmetic operation must be the same. When the operands involved in the operation are of different data types, the operands involved in the operation need to be converted to the same data type before operation.

Such as:

Double PI = 3.14; int radius = 5; double s; s = PI * radius * radius;

In the example above, the expression PI * radius * radius has two operands, PI being a double and radius being an integer. Because the operands are of inconsistent data types, the Java compiler casts radius integers to double.

This type of conversion is performed automatically by the Java compiler without the programmer doing anything. This type of conversion is also called implicit conversion.

Because the storage space and numerical precision of different data types are different, the problems of numerical precision loss and numerical overflow will occur in the process of data type conversion. For example, when a double is converted to a float, numerical precision is lost; When a long is converted to an int, an overflow occurs if the long variable stores more values than an int can store.

In order to avoid the problem of numerical precision loss and overflow in the conversion process, the implicit conversion follows the conversion rules from low-level data types to high-level data types, which can also be said to be the conversion from the type with low value storage precision to the type with high value storage precision. These data types are in order of numeric storage range:

Byte ->short->int->long->float->double The following table lists the general rules for implicit conversions of data types:

Case 1: Declare variables of different types, perform arithmetic operations between variables, and examine the implicit conversion of types during the operation. The code is as follows:

/ * *

  • @Title: DataTypeChangeSample.java
  • @Package unit
  • @description: Java Basics course case
  • @author Programming Boot camp
  • Date December 2, 2019
  • @ version V1.0

* /

package unit;

/ * *

  • @ClassName: DataTypeChangeSample
  • @description: Data type conversion (implicit conversion) case 1
  • @author Programming Boot camp
  • Date December 2, 2019

* /

public class DataTypeChangeSample {

/** * @Title: main * @Description: Java program entry Main method * @param @param args parameter * @return void Return type * @throws */ public static void main(String[] args) {// Declare a variable of type byte and initialize it to the maximum value byte tempByte = 65. // Declare a variable of type int tempInt = 34; // Declare a variable of type float float tempFloat = 29.6f; // Declare a variable of type double double tempDouble = 86.69; Println (" + (tempByte+tempFloat)); system.out. println(" + (tempByte+tempFloat)); System.out.println(" + (tempFloat+tempDouble) + (tempFloat+tempDouble)); System.out.println(" Add byte data to int data and the result is: "+ (tempByte+tempInt)); }Copy the code

}

Display type conversion

Display type conversions are opposed to implicit conversions, which are performed automatically by the Java compiler and require no programmer action. Display type conversions require programmers to perform display type conversions on data types in code.

When performing display conversion of data types, programmers need to judge by themselves whether there will be numerical overflow or precision loss in the conversion process. When converting from a high precision type to a low precision type, precision loss will occur.

The syntax for the conversion is shown as follows:

(type name) The variable or constant to be converted

Such as:

Int tempInt = (int)36.9; // Declare a variable of type double double tempDouble = 12.15; // Cast double to int, loss of precision tempInt = (int)tempDouble; In the example above, 36.9 is a numeric constant, of type double by default, which is converted to an int and assigned to a tempInt, where the value of tempInt is 36 and the precision is lost.

A display conversion is required when assigning the value of tempDouble to an int, which stores only the integer part of the double; the decimal part is lost.

All conversions between primitive types except Boolean types can be implemented using display conversions.