package cn.test;
/**
* 介绍基本包装类的一些基本操作
* int Integer
* char Character
* 其他的包装类除了首字母大写外和基本数据类型名相同。
* 自动拆箱: 将包装类 --> 基本类型
* 自动装箱: 基本类型 --> 包装类
* 包装类的方法都是static的,可以直接类名调用
*/
public class OperationsOfPackagingGroup {
public static void main(String[] args) {
integerTest();
characterTest();
}
/**
* 介绍int - Integer 的基本用法
*/
public static void integerTest() {
// 构造方法可以传int类型, 也可以是字符串类型:
Integer a = new Integer(11);
Integer b = new Integer("11");
// equals判等
System.out.println("a == b?" + a.equals(b));
}
/**
* 以下仅列举Character的一些常用方法
*/
public static void characterTest() {
// 我们可以用Character类的方法操作char类型数据,比如一些常用的判断
char[] a = {'a', 'c', 'A', '1', 2, 97, '.', '-', ' '};
/**
* isDigit(char c) bool 判断字符是否为数字
* isLetter(char c) bool 判断字符是否为字母
* isLowerCase(char c) bool 判断字符是否为小写字母
* isUpperCase(char c) bool 判断字符是否为大写字母
* isWhiteSpace(char c) bool 判断字符是否为空白字符
*/
for (char c : a) {
if (Character.isDigit(c)) {System.out.println(c + " isDigit");}
// else if (Character.isLetter(c)) {System.out.println(c + "-isLetter");}
else if (Character.isLowerCase(c)) {System.out.println(c + " isLowerCase");}
else if (Character.isUpperCase(c)) {System.out.println(c + " isUpperCase");}
else {
System.out.println(c + " 其他字符");
}
}
/**
* a == b?true
*
* a isLowerCase
* c isLowerCase
* A isUpperCase
* 1 isDigit
* 其他字符
* a isLowerCase
* . 其他字符
* - 其他字符
* 其他字符
* Process finished with exit code 0
*/
}
}
包装类简单使用
最新推荐文章于 2026-01-04 14:19:16 发布
本文详细介绍了Java中的包装类,如Integer和Character,以及它们与基本类型之间的自动装箱和拆箱操作。通过示例展示了如何使用Integer的构造方法和Character的常用方法,并提供了相关的方法调用示例,帮助读者深入理解这些概念。
681

被折叠的 条评论
为什么被折叠?



