Java 中将 Int 转换为 Integer
本文介绍了如何在 Java 中将原始 int 转换为 Integer 对象。
Java 使用原始 int
类型或 Integer
包装类来保存整数值。如果我们想将原始 int 转换为 Integer
对象,Java 提供了几种方法,例如 valueOf()
和 Integer()
构造函数。
在本文中,我们将学习使用这些方法。所以,让我们开始吧。
在 Java 中使用自动装箱将 Int 转换为 Integer
自动装箱是一种将原始类型隐式转换为对象的技术。它的反转称为拆箱。Java 隐式支持自动装箱,因此我们不需要编写任何额外的代码。
在下面的示例中,我们使用自动装箱将 int 转换为 Integer 对象,并看到两个变量具有相同的值。请参阅下面的代码示例。
public class SimpleTesting {
public static void main(String[] args) {
int a = 10;
System.out.println("a = " + a);
Integer i = a; // autoboxing
System.out.println("i = " + i);
}
}
输出:
a = 10
i = 10
在 Java 中使用整数构造函数将 int 转换为 Integer
Java Integer
类是一个包装类,用于创建原始 int 类型的对象。我们可以使用它的构造函数将 int 转换为 Integer 对象。在下面的示例中,我们使用了 Integer
类构造函数,它将 int 值作为参数并将其作为 Integer 对象返回。
public class SimpleTesting {
public static void main(String[] args) {
int a = 10;
System.out.println("a = " + a);
Integer i = new Integer(a);
System.out.println("i = " + i);
}
}
输出:
a = 10
i = 10
使用 Java 中的 Integer.valueOf()
方法将 int 转换为 Integer
这是我们可以用来在 Java 中将 int 转换为 Integer 的另一个。在这里,我们使用了 Integer
类的 valueOf()
方法。它是一个静态方法,它接受一个 int 原始参数并返回一个 Integer 对象。所以,我们可以在这里使用这个方法。请参阅下面的代码示例。
public class SimpleTesting {
public static void main(String[] args) {
int a = 10;
System.out.println("a = " + a);
Integer i = Integer.valueOf(a);
System.out.println("i = " + i);
}
}
输出:
a = 10
i = 10
我们已经看到了如何在 Java 中将一个 int 转换为一个 Integer,但这里我们给你提供了另一个技巧来验证转换是否成功。这意味着你可以使用 Object
类的 getClass()
方法来验证结果。此方法返回类的完全限定名称(包括包名称)。
我们使用 getClass()
方法来检查结果值是否是 Integer 类的对象,并查看它是否正常工作。
我们使用 getSimpleName()
方法仅从完全限定名称中获取类的名称。请参阅下面的示例。
public class SimpleTesting {
public static void main(String[] args) {
int a = 10;
System.out.println("a = " + a);
Integer i = Integer.valueOf(a);
System.out.println("i = " + i);
System.out.println(i.getClass().getSimpleName());
}
}
输出:
a = 10
i = 10
Integer
如果我们不使用 getSimpleName()
方法,输出结果将是如下所示。
a = 10 i = 10 class java.lang.Integer
相关文章
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 系列日期时间转换为字符串
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
如何在 Pandas DataFrame 的列中将所有 NaN 值替换为零
发布时间:2024/04/23 浏览次数:198 分类:Python
-
在 Pandas 库中使用 df.fillna(),df.replace()方法在 DataFrame 中将 NaN 值替换为零
Pandas DataFrame DataFrame.interpolate()函数
发布时间:2024/04/22 浏览次数:214 分类:Python
-
DataFrame interpolate()函数使用插值技术填充 DataFrame 中的 NaN 值。
在 Pandas DataFrame 中将多列中的值合并为一列
发布时间:2024/04/21 浏览次数:142 分类:Python
-
本教程演示了将多列中的第一个非空值返回到 Python Pandas DataFrame 中的另一列。