基本类型包装类
概述
将基本数据类型封装成对象,便于我们使用。Java就为每种基本类型提供了对应的包装类类型。
对应类型
基本数据类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
如何使用
参考阿里巴巴开发手册中的 OOP规约
13. 关于基本数据类型与包装数据类型的使用标准如下:
1) 【强制】所有的POJO类属性必须使用包装数据类型。
2) 【强制】RPC方法的返回值和参数必须使用包装数据类型。
3) 【推荐】所有的局部变量使用基本数据类型。
说明:POJO类属性没有初值是提醒使用者在需要使用时,必须自己显式地进行赋值,任何NPE问题,或者入库检查,都由使用者来保证。
正例:数据库的查询结果可能是null,因为自动拆箱,用基本数据类型接收有NPE风险。
反例:某业务的交易报表上显示成交总额涨跌情况,即正负x%,x为基本数据类型,调用的RPC服务,调用不成功时,返回的是默认值,页面显示为0%,这是不合理的,应该显示
Integer
Integer
类在对象中包装了一个基本类型 int
的值。Integer
类型的对象包含一个 int
类型的字段。
此外,该类提供了多个方法,能在 int
类型和 String
类型之间互相转换,还提供了处理 int
类型时非常有用的其他一些常量和方法。
常量
// int最大值
public static final int MIN_VALUE = 0x80000000;
// int最小值
public static final int MAX_VALUE = 0x7fffffff;
案例代码
public class IntegerFieldDemo {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
构造方法
// 将int类型转为Integer类型
public Integer(int value) {}
// 将String类型转为Integer类型
public Integer(String s) throws NumberFormatException {}
案例代码
public class ConstructorDemo {
public static void main(String[] args) {
Integer num1 = new Integer(100);
Integer num2 = new Integer("100");
System.out.println(num1);
System.out.println(num2);
}
}
类型和进制转换方法
// Integer转为byte类型
public byte byteValue()
// Integer转为short类型
public short shortValue()
// Integer转为int类型
public int intValue()
// Integer转为double类型
public double doubleValue()
// int转为二进制类型的String类型
public static String toBinaryString(int i)
// int转为八进制类型的String类型
public static String toOctalString(int i)
// int转为十六进制类型的String类型
public static String toHexString(int i)
// 将String类型转为int类型
public static int parseInt(String s)
// 将int转为Integer
public static Integer valueOf(int i)
// 将String转为Integer
public static Integer valueOf(String s)
案例代码
public class ConvertMethodDemo {
public static void main(String[] args) {
Integer num = new Integer(100);
byte byteValue = num.byteValue();
double doubleValue = num.doubleValue();
int intValue = num.intValue();
String binaryString = Integer.toBinaryString(100);
String hexString = Integer.toHexString(100);
String octalString = Integer.toOctalString(100);
int parseInt = Integer.parseInt("100");
Integer valueOf = Integer.valueOf(100);
Integer valueOf2 = Integer.valueOf("100");
System.out.println(byteValue);
System.out.println(doubleValue);
System.out.println(intValue);
System.out.println(binaryString);
System.out.println(hexString);
System.out.println(octalString);
System.out.println(parseInt);
System.out.println(valueOf);
System.out.println(valueOf2);
}
}
自动拆装箱
Java 5 增加了自动装箱、拆箱机制,提供基本数据类型和包装类型的相互转换操作。
自动装箱即自动将基本数据类型转换成包装类型
自动拆箱即自动将包装类型转换成基本数据类型
public class TestInteger {
public static void main(String[] args) {
int num1 = 10;
// 自动装箱
Integer num2 = num1;
// 自动拆箱
num2++;
System.out.println(num2); // 11
}
}
Character
常用方法
// 判断是否是大写字符
public static boolean isUpperCase(char ch)
// 判断是否是小写字符
public static boolean isLowerCase(char ch)
// 判断是否是数字
public static boolean isDigit(char ch)
// 字符转大写
public static char toUpperCase(char ch)
// 字符转小写
public static char toLowerCase(char ch)
总结
基本类型包装类是对基本数据类型的封装,并提供了一些字段和方法使其更符合面向对象思想
除了Integer和Character,其他6种都是首字母大写