迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中强制转换变量

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍如何在 Java 中强制转换变量或将变量转换为另一种类型。

转换用于在编程语言中将一个值/变量转换/转换为另一种类型。Java 支持丰富的数据类型,如 intfloatdoubleboolean 等,在编写代码时,可能需要转换变量。

Java 支持两种类型的转换,隐式转换和显式转换。如果我们将 int 转换为 longdouble,那么 Java 会隐式执行此操作,因为 longdouble 使用的字节数比 int 多,我们会执行相反的操作(即,将 double 转换为 int) 那么它可能会由于整数容量而导致数据丢失。

因此,Java 允许在较低数据类型到较高数据类型之间进行隐式转换,但在较高数据类型到较低数据类型之间进行显式转换。让我们通过一些例子来理解。


Java 中的隐式转换

在这里,在这个例子中,我们将 int 转换为 float 类型,隐式转换。由于 Java 允许在没有太多代码的情况下进行隐式转换,因此该代码可以正常工作。

public class SimpleTesting {
  public static void main(String[] args) {
    int a = 23;
    System.out.println("int " + a);
    // int to float - implicit casting
    float f = a;
    System.out.println("float " + f);
  }
}

输出:

int 23
float 23.0

Java 中的显式转换

在某些情况下,由于数据丢失,Java 确实需要显式转换。

例如,如果我们将 float 转换为 int,那么小数点后的值会被截断;这就是 Java 不隐式执行此操作并引发编译时错误的原因。请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    float a = 23;
    System.out.println("float " + a);
    // float to int - explicit casting
    int f = a;
    System.out.println("int " + f);
  }
}

输出:

Type mismatch: cannot convert from float to int

这个编译时错误是对程序员的警告,以避免这种数据丢失。如果程序员仍想进行类型转换,Java 允许使用类型名称将类型名称括在函数括号中的类型转换运算符。

此编译器编译并执行代码,但看到值被截断。请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    float a = 23.5f;
    System.out.println("float " + a);
    // float to int - explicit casting
    int f = (int) a;
    System.out.println("int " + f);
  }
}

输出:

float 23.50
int 23

我们还可以使用 cast() 方法将对象值转换为原始值。请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    Integer a = 23;
    System.out.println("integer " + a);
    // float to int - explicit casting
    int i = (Integer.class.cast(a));
    System.out.println("int " + i);
  }
}

输出:

integer 23
int 23

在 Java 中避免 CastException

转换 Java 对象也是一个主要问题,在转换之前需要正确的类类型。例如,如果我们用 double 强制转换 float 类型,Java 不允许这样做并将 ClassCastException 抛出到控制台。

请参见下面的示例。

public class SimpleTesting {
  public static void main(String[] args) {
    Number num = new Float(15.5);
    System.out.println(num);
    Double d = (Double) num;
    System.out.println(d);
  }
}

输出:

15.5
Exception in thread "main" java.lang.ClassCastException

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便