包装类
在 Java 的设计中提倡一种思想,即一切皆对象。但是从数据类型的划分中,我们知道 Java 中的数据类型分为基本数据类型和引用数据类型,但是基本数据类型怎么能够称为对象呢?于是 Java 为每种基本数据类型分别设计了对应的类,称之为包装类(Wrapper Classes),也有地方称为外覆类或数据类型类。
基本数据类型(默认值) | 包装类(默认值) | 取值范围 |
---|---|---|
byte (0) | Byte (null) | -128~127 |
short (0) | Short (null) | -32768~32767 |
int (0) | Integer (null) | -231 ~ 2(31-1) |
long (0) | Long (null) | -263 ~ 2(63-1) |
float (0.0) | Float (null) | 1.4E-45~3.4028235E38 |
double (0.0) | Double (null) | 4.9E-324~1.7976931348623157E308 |
boolean (false) | Boolean (null) | true或false |
char (为空) | Character (null) | 0~65535 |
包装类的作用就是把基本数据类型转换为对象
每个基本类型在java.lang包中都有一个相应的包装类
包装类的作用
-
作为基本数据类型对应的类存在,方便有关对象的操作
-
可以用设定每种基本数据类型的属性大小及使用方法
包装类型的继承结构:
包装类的构造方法
所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
public Type(type value)
如:Integer i=new Integer(1);
//除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
public Type(String value);
如: Integer i=new Integer("123");
- Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false。
- 当包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常。
包装类的常用方法
装箱:基本数据类型转换为包装类的对象
拆箱:包装类对象转换为基本类型的值
XXXValue():
包装类型转换为基本类型
Integer integerld = new Integer(25);
int intld = integeld.intValue();
parseXXX():
把字符串转换为相应的基本数据类型数据(Character除外)
int num = Integer.parseInt("36");
boolean = Boolean.parseBoolean("false");
基本类型和包装类的自动转换
Integer intObject = 5;
int intValue = intObject.intValue();
装箱和拆箱:
/**
* 装箱和拆箱
*/
@Test
public void testInt() {
Integer numOne = 127;
Integer numTwo = new Integer("127");
Integer numThree = new Integer(128);
Integer numFour = Integer.parseInt("128");
Integer numFive = 126;
Integer numSix = 126;
//false,Integer---是对象类型,比较的是内存地址
System.out.println(numOne == numTwo);
//false
System.out.println(numThree == numFour);
//true
System.out.println(numFive == numSix);
}
//运行结果:
// false
// false
// true
原因如下:
在Integer类装载入内存时,把[-128, 127]范围内的整型数据装包成Integer类,并将其对应的引用放入到cache数组中。
从上面的源码可以看出,valueOf()在返回之前,会进行判断,判断当前 i的值是否在 -128到127之间。
如果存在,则直接返回引用,不再重新开辟内存空间。
如果不存在,就创建一个新的对象。
利用缓存,这样做既能提高程序执行效率,还能节约内存。
Integer a1= 127; Integer a2 = 127; 因为 IntegerCache中已经存在此对象,直接返回引用,引用相等并且都指向缓存中的数据,所以这时候a1 == a2返回true。
Integer a1 = 128; Integer a2 = 128;因为a1,a2的值大于127,不在[-128, 127]范围内,所以虚拟机会在堆中重新new一个 Integer对象来存放128,创建两个对象就会产生两个这样的空间。两个空间的地址不同,返回到栈中的引用的值也就不同,所以这时候a1 == a2返回false。
字符串提取方法
方法名 | 说明 |
---|---|
public int indexOf(int ch) public int indexOf(String value) | 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1 |
public int lastIndexOf(int ch) public int lastIndexOf(String value) | 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1 |
public String substring(int index) | 提取从位置索引开始的字符串部分 |
public String subdtring(int beginindex, int endindex) | 提取beginindex和endindex之间的字符转部分 |
public String trim() | 返回一个前后不含任何空格的调用字符串的副本 |
StringBuffer
对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率.
StringBuffer声明
StringBuffer strb = new StringBuffer();
StringBuffer strb = new StringBuffer("aaa");
StringBuffer 的使用
sb.toString();//转化为String类型
sb.append("*");//追加字符串
sb.insert(1, "*");//插入字符串
值得一提的是:String 是不可变对象
经常改变内容的字符串最好不要使用String
StringBuffer是不可变的字符串
字符串经常改变的情况可以使用StringBuffer,更高效
JDK1.5后提供了StringBuilder,等价StringBuffer