StringBuffer类概述
- 我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题。
- 他是一个长度可变的字符容器,可以存储多个字符,可以不断的往容器中追加内容。
- 线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。但通过某些方法调用可以改变该序列的长度和内容。
StringBuffer的方法:
StringBuffer的构造方法:
public StringBuffer(): 无参构造方法
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象,初始容量16个字节
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
StringBuffer的添加功能:
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str): 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
StringBuffer的删除功能:
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容(含头不含尾),并返回本身
StringBuffer的替换功能:
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
StringBuffer的反转功能:
public StringBuffer reverse(): 字符串反转
StringBuffer 检索的功能:
int indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引。
int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
int lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。
int lastIndexOf(String str, int fromIndex) 返回最后一次出现的指定子字符串在此字符串中的索引。
StringBuffer的截取功能:
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
注意:返回值类型不再是StringBuffer本身
StringBuffer和String的相互转换
- String – StringBuffer
通过构造方法
通过append()方法 - StringBuffer – String
使用substring方法
通过构造方法
通过toString()方法
代码实现:
public class MyTest {
public static void main(String[] args) {
// String ---- StringBUffrer
String str = "abc";
//方式1
StringBuffer sb = new StringBuffer(str);
System.out.println(sb);//abc
//方式二
StringBuffer sb2 = new StringBuffer("aaa bbb ccc").append(str);
StringBuffer sb3 = sb2.insert(0, str);
System.out.println(sb3);//abcaaa bbb cccabc
StringBuffer sb4 = sb2.replace(0, sb2.length(), str);
System.out.println(sb4);
System.out.println("----");
//StringBuffer----String
StringBuffer sb5= new StringBuffer("abc");
//方式1
String string = sb5.toString();
System.out.println(string);
//方式2
String substring = sb5.substring(0);
System.out.println(substring);
//方式3
//String(StringBuffer buffer)
//分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。
String s = new String(sb5);
System.out.println(s);
}
}
运行结果:
abc
abcaaa bbb cccabc
abc
----
abc
abc
abc
StringBuffer和StringBuilder的区别
StringBuffer 字符串缓冲区 线程安全, 效率di
StringBuilder 字符串缓冲区 线程不安全 效率低
Arrays类的方法使用
public static String toString(int[] a) 打印
public static void sort(int[] a) 升序排序
public static int binarySearch(int[] a,int key) 二分查找
这些方法的状态修饰符都是static,所以都是静态方法,可以通过类名进行调用
基本类型包装类的概述
基本类型 | 包装类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer类的概述和构造方法
Integer类概述:
Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个方法,
能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时
非常有用的其他一些常量和方法
构造方法
public Integer(int value)
public Integer(String s)
判断一个数据是否在int的范围内
boolean b= 100000>=Integer.MIN_VALUE&& 100000<=Integer.MAX_VALUE?true:false;
//100000大于int的最小值且效益int的最大值
System.out.println(b);
代码实现:
public class MyTest {
public static void main(String[] args) {
//将100转换成二进制, 八进制, 十六进制
//判断一个数是否在int的范围内
//java为了我们更好地操作基本数据类型,给我们提供了与之对应的包装类型
int num = 100;
String string = Integer.toBinaryString(num);//转换成二进制
String string1 = Integer.toOctalString(num);//转换成八进制
String string2 = Integer.toHexString(num);//转换成十六进制
System.out.println(string);
System.out.println(string1);
System.out.println(string2);
System.out.println(new Object().toString());//java.lang.Object@140e19d
boolean b= 100000>=Integer.MIN_VALUE&& 100000<=Integer.MAX_VALUE?true:false;
System.out.println(b);//true
}
}
运行结果:
1100100
144
64
java.lang.Object@140e19d
true
String和int类型的相互转换
int --> String
1. 和""进行拼接
2. public static String valueOf(int i)
3. int -- Integer -- String
4. public static String toString(int i)
String --> int
1. String -- Integer -- intValue();
2. public static int parseInt(String s)
public class MyTest {
public static void main(String[] args) {
//int --- String
//方法1
int num = 100;
String str = num +"";
System.out.println(str);
//方法2
String s = String.valueOf(num);
System.out.println(s);
//方法3
String string2 = new Integer(num).toString();
System.out.println(string2);
//String ---- int
String st = "200";
//方式1
Integer integer = new Integer(st);
int i = integer.intValue();
System.out.println(i);
//方式2
int i1 = Integer.parseInt(st);
System.out.println(i1);
}
}
JDK5的新特性自动装箱和拆箱
自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型
public class MyTest {
public static void main(String[] args) {
Integer i = 100;//自动装箱
System.out.println(i);
i += 10;//自动拆箱。自动装箱
System.out.println(i);
Integer integer = new Integer(1000);
int i1 = integer.intValue();//手动拆箱
System.out.println(i1);
System.out.println(integer);
Integer integer1 = new Integer(1000);//手动装箱
Integer integer2 = new Integer("1000");
System.out.println(integer1);
System.out.println(integer2);
}
}
注意:在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。
Integer的面试题
public class MyTest2 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
//Integer重写了父类的equals方法,比较的是值是否相等
System.out.println(i1.equals(i2));//true
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);//false
System.out.println(i3.equals(i4));//true
System.out.println("-----------");
//这个值,超过了一个字节的范围,回去重新new一个Integer对象
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false
System.out.println(i5.equals(i6));//true
System.out.println("-----------");
// Integer integer = Integer.valueOf(127);
//没有超过一个字节 会从缓存里面取出一个对象
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true
System.out.println(i7.equals(i8));//true
System.out.println("-----");
Integer a = new Integer(127);
Integer b = 127;
System.out.println(a == b);//false
}
}
运行结果:
false
true
-----------
false
true
-----------
false
true
-----------
true
true
---
false
- Integer与Integer比较的时候,由于直接赋值的时候会进行自动的装箱,那么这里就需要注意两个问题,一个是-128<= x<=127的整数,将会直接缓存在IntegerCache中,那么当赋值在这个区间的时候,不会创建新的Integer对象,而是从缓存中获取已经创建好的Integer对象。二:当大于这个范围的时候,直接new Integer来创建Integer对象。
- new Integer(1) 和Integer a = 1不同,前者会创建对象,存储在堆中,而后者因为在-128到127的范围内,不会创建新的对象,而是从IntegerCache中获取的。那么Integer a = 128, 大于该范围的话才会直接通过new Integer(128)创建对象,进行装箱。