Java 简明教程

Java - Type Casting

Java Type Casting (Type Conversion)

Type casting 是一种由编译器或程序员用来转换一个 data type 到另一个 data type 的技术。例如,将 int 转换为 double、double 转换为 int、short 转换为 int,等等。类型类型转换也称为类型转换。

Type casting is a technique that is used either by the compiler or a programmer to convert one data type to another. For example, converting int to double, double to int, short to int, etc. Type typing is also known as Type conversion.

在 Java 编程中允许两种类型的强制类型转换:

There are two types of cast typing allowed in Java programming:

  1. Widening type casting

  2. Narrowing type casting

Widening Type Casting

Widening Type Casting 也称为 implicit type casting,其中一个较小的类型被转换为一个较大的类型,它是由编译器自动完成的。

Widening Type Casting is also known as implicit type casting in which a smaller type is converted into a larger type, it is done by the compiler automatically.

以下是 Java 中类型提升强制转换的层级:

Here is the hierarchy of widening type casting in Java:

byte > short > char > int > long > float > double

编译器在类型转换中发挥作用,但是程序员不发挥作用。它在编译时更改 variables 的类型。此外,类型转换仅从小型数据类型转换到大型数据类型。

The compiler plays a role in the type conversion but not the programmers. It changes the type of the variables at the compile time. Also, type conversion occurs from the small data type to large data type only.

Example 1: Widening Type Casting

在下面的示例中,我们演示了当编译器尝试将一个大的数据类型转换为一个小的数据类型时,我们可能会得到一个错误。这里,我们创建了一个 num1 整数和 num2 double 变量。num1 和 num2 的和将是 double,当我们尝试将其存储到 int 类型和中时,编译器将给出一个错误。

In the example below, we demonstrated that we can get an error when the compiler tries to convert a large data type to a small data type. Here, we created the num1 integer and num2 double variable. The sum of num1 and num2 will be double, and when we try to store it to the sum of the int type, the compiler gives an error.

package com.tutorialspoint;

public class Tester {
   // Main driver method
   public static void main(String[] args) {
      // Define int variables
      int num1 = 5004;
      double num2 = 2.5;
      int sum = num1 + num2;
      // show output
      System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
   }
}

编译并运行 Tester。这将产生以下结果:

Compile and run Tester. This will produce the following result −

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
	Type mismatch: cannot convert from double to int

	at com.tutorialspoint.Tester.main(Tester.java:9)

Example 2: Widening Type Casting

在下面的示例中,我们创建了 num1 和 num2 变量,与第一个示例中相同。我们将两个数字的和存储在 double 类型的 sum 变量中。在输出中,我们可以看到 double 类型的和。

In the example below, we created the num1 and num2 variables as same in the first example. We store the sum of both numbers in the sum variable of double type. In the output, we can observe the sum of double type.

package com.tutorialspoint;

public class Tester {
   // Main driver method
   public static void main(String[] args) {
      // Define int variables
      int num1 = 5004;
      double num2 = 2.5;
      double sum = num1 + num2;
      // show output
      System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
   }
}

编译并运行 Tester。这将产生以下结果:

Compile and run Tester. This will produce the following result −

The sum of 5004 and 2.5 is 5006.5

Narrowing Type Casting

Narrowing type casting 也称为 explicit type castingexplicit type conversion,由程序员手动完成。在变窄强制类型转换中,一个较大的类型可以被转换为一个较小的类型。

Narrowing type casting is also known as explicit type casting or explicit type conversion which is done by the programmer manually. In the narrowing type casting a larger type can be converted into a smaller type.

当程序员在编写代码时改变了变量的类型。我们可以使用强制转换运算符来改变变量的类型。例如,double 转换为 int 或 int 转换为 double。

When a programmer changes the variable type while writing the code. We can use the cast operator to change the type of the variable. For example, double to int or int to double.

Syntax

下面是强制类型转换的语法,即手动类型转换:

Below is the syntax for narrowing type casting i.e., to manually type conversion:

double doubleNum = (double) num;

上面的代码语句将变量转换为 double 类型。

The above code statement will convert the variable to double type.

Example: Narrowing Type Casting

在下面的示例中,我们定义了一个整数类型的 num 变量,并用值对其进行初始化。而且,我们定义了一个 double 类型的 doubleNum 变量,并在将 num 变量转换为 double 之后存储它的值。

In the example below, we define the num variable of integer type and initialize it with the value. Also, we define the doubleNum variable of double type and store the num variable’s value after converting it to the double.

接下来,我们创建了 'convertedInt' 整数类型变量,并在类型强制转换为 int 之后存储 double 值。在输出中,我们可以看到 double 和 int 变量的值。

Next, We created the 'convertedInt' integer type variable and stored the double value after type casting to int. In the output, we can observe the value of the double and int variables.

package com.tutorialspoint;

public class Tester {
   // Main driver method
   public static void main(String[] args) {
      // Define int variable
      int num = 5004;
      // Type casting int to double
      double doubleNum = (double) num;
      // show output
      System.out.println("The value of " + num + " after converting to the double is " + doubleNum);
      // Type casting double to int
      int convertedInt = (int) doubleNum;
      // show output
      System.out.println("The value of " + doubleNum + " after converting to the int again is " + convertedInt);
   }
}

编译并运行 Tester。这将产生以下结果:

Compile and run Tester. This will produce the following result −

The value of 5004 after converting to the double is 5004.0
The value of 5004.0 after converting to the int again is 5004