slug
wrapperclasses
type
Post
status
Published
category
AP CSA
date
May 27, 2025
summary
tags
concepts
password
icon
In Java, wrapper classes are used to wrap primitive data types into objects. Each primitive type has a corresponding wrapper class in the
java.lang package. This is useful when working with collections, which can only store objects (not primitives), or when using object-oriented features such as generics, method overloading, etc.🔹 List of Wrapper Classes in Java
Primitive Type | Wrapper Class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
🔸 Example 1: Converting primitive to wrapper (Boxing)
🔸 Example 2: Converting wrapper to primitive (Unboxing)
🔸 Example 3: Autoboxing and Autounboxing (automatic conversion)
🔸 Example 4: Using wrapper class in collections
🔸 Example 5: Parsing strings to numbers
✅ Key Benefits of Wrapper Classes
- Used in collections like
ArrayList,HashMap, etc.
- Helpful for converting between strings and numbers.
- Enables use of null values for primitives in object form.
- Useful when working with generics (which require objects).
当然可以,以下是对 Java 中 包装类(Wrapper Class) 的中文解释和示例。
🔹 Java 中的包装类(Wrapper Classes)
Java 中的包装类用于将基本数据类型(primitive types)封装成对象。这样可以让基本类型在需要对象的场景中使用,例如集合框架(如
ArrayList)和泛型等。🔸 基本数据类型与对应的包装类
基本类型 | 包装类 |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
🔸 示例 1:基本类型转换为包装类(装箱 Boxing)
🔸 示例 2:包装类转换为基本类型(拆箱 Unboxing)
🔸 示例 3:自动装箱与自动拆箱
🔸 示例 4:在集合中使用包装类
🔸 示例 5:将字符串转换为数字
✅ 包装类的好处总结:
- 在集合(如
ArrayList)中使用(集合只能存对象)
- 方便将字符串转换为数字
- 可用于泛型等只能处理对象的结构
- 可以表示
null值,适合某些特定应用场景
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/wrapperclasses
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章




