在 Java 中强制转换变量
本文介绍如何在 Java 中强制转换变量或将变量转换为另一种类型。
转换用于在编程语言中将一个值/变量转换/转换为另一种类型。Java 支持丰富的数据类型,如 int
、float
、double
、boolean
等,在编写代码时,可能需要转换变量。
Java 支持两种类型的转换,隐式转换和显式转换。如果我们将 int
转换为 long
或 double
,那么 Java 会隐式执行此操作,因为 long
和 double
使用的字节数比 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
相关文章
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
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。