一、StringBuffer和StringBuiler的区别
StringBuffer和StringBuilder是字符串的缓冲区。
StringBuffer:线程安全,效率低
StringBuilder:线程不安全的,效率高
StringBuffer和StringBuiler的使用:
1,存储。
StringBuffer append():将指定数据作为参数添加到已有数据结尾处。
StringBuffer insert(index,数据):可以将数据插入到指定index位置。
2,删除。
StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。
StringBuffer deleteCharAt(index):删除指定位置的字符。
3,获取。
char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
4,修改。
StringBuffer replace(start,end,string);
void setCharAt(int index, char ch) ;
5,反转。
StringBuffer reverse();
6,
将缓冲区中指定数据存储到指定字符数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
StringBuffer append():将指定数据作为参数添加到已有数据结尾处。
StringBuffer insert(index,数据):可以将数据插入到指定index位置。
2,删除。
StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。
StringBuffer deleteCharAt(index):删除指定位置的字符。
3,获取。
char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
4,修改。
StringBuffer replace(start,end,string);
void setCharAt(int index, char ch) ;
5,反转。
StringBuffer reverse();
6,
将缓冲区中指定数据存储到指定字符数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
由于StringBuffer和StringBuilder的用法相同所以StringBuilder就不一一列出
下面是关于StringBuffer和StringBuilder的一部分代码:
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
//默认构造函数
StringBuffer buf1 = new StringBuffer();
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//0
//字符串的构造函数
StringBuffer buf2 = new StringBuffer("hello");
System.out.println("buf2容量:" + buf2.capacity());//21
System.out.println("buf2长度:" + buf2.length());//5
//指定初始容量
StringBuffer buf3 = new StringBuffer(20);
System.out.println("buf3容量:" + buf3.capacity());//20
System.out.println("buf3长度:" + buf3.length());//0
}
}
/**
* @param args
*/
public static void main(String[] args) {
//默认构造函数
StringBuffer buf1 = new StringBuffer();
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//0
//字符串的构造函数
StringBuffer buf2 = new StringBuffer("hello");
System.out.println("buf2容量:" + buf2.capacity());//21
System.out.println("buf2长度:" + buf2.length());//5
//指定初始容量
StringBuffer buf3 = new StringBuffer(20);
System.out.println("buf3容量:" + buf3.capacity());//20
System.out.println("buf3长度:" + buf3.length());//0
}
}
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer buf1 = new StringBuffer();//容量:16,长度:0
buf1.append("hello");//hello
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5
buf1.append(100);//hello100
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8
buf1.append(3.14);//hello1003.14
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8 + 4 = 12
buf1.append(true);//hello1003.14true
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8 + 4 = 12 + 4 = 16
buf1.append("a");//hello1003.14truea
System.out.println("容量:" + buf1.capacity());//34(原容量 * 2 + 2)
System.out.println("长度:" + buf1.length());//16 + 1 = 17
System.out.println(buf1.toString());//hello1003.14truea
//2.StringBuffer insert(int offset, 任意类型)
StringBuffer buf2 = new StringBuffer();
buf2.append("helloworld");
// buf2.insert(1, "xxx");
// buf2.insert(10, "xxx");//helloworldxxx
// buf2.insert(11, "xxx");//运行时异常: java.lang.StringIndexOutOfBoundsException:offset一定要在 <= length()
System.out.println(buf2);
//3.StringBuffer delete(int start, int end)
StringBuffer buf3 = new StringBuffer();
buf3.append("hello");
// buf3.delete(1, 3);
// buf3.delete(3, 1);//java.lang.StringIndexOutOfBoundsException
// buf3.delete(3, 3);//如果start等于end时,不发生任何更改
buf3.delete(1, 500);//end如果超出范围,不抛异常,会截取到结尾
// buf3.delete(5, 5);//start的范围 < length(),否则运行时异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf3);
//4.StringBuffer deleteCharAt(int index)
StringBuffer buf4 = new StringBuffer();
buf4.append("hello");
buf4.deleteCharAt(1);
buf4.deleteCharAt(5);//索引参数一定要 < length() :运行时异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf4);
//5.StringBuffer replace(int start, int end, String str)
StringBuffer buf5 = new StringBuffer();
buf5.append("hello");
// buf5.replace(1, 3, "ELXXXXXXZ");
//buf5.replace(1, 30, "ELXXXXXXZ");//end超出范围,不抛异常,将截取到字符末尾;
//buf5.replace(1,1, "ELL");//hELLello
//buf5.replace(6, 30, "XXX");//start <= length() 否则异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf5);
//6.String substring(int start):
StringBuffer buf6 = new StringBuffer();
buf6.append("hello");
String str = buf6.substring(5);
//String str = buf6.substring(6);//start <= length() 否则异常:java.lang.StringIndexOutOfBoundsException
System.out.println("str = " + str);
//7.String substring(int start, int end)
StringBuffer buf7 = new StringBuffer();
buf7.append("hello");
String str2 = buf7.substring(1,3);
/ /String str2 = buf7.substring(5,5);//start,end <= length() 否则异常: java.lang.StringIndexOutOfBoundsException
System.out.println("str2 = " + str2);
//8.StringBuffer reverse()
StringBuffer buf8 = new StringBuffer();
buf8.append("hello");
buf8 = buf8.reverse();
System.out.println("buf8 = " + buf8);
}
}
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer buf1 = new StringBuffer();//容量:16,长度:0
buf1.append("hello");//hello
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5
buf1.append(100);//hello100
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8
buf1.append(3.14);//hello1003.14
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8 + 4 = 12
buf1.append(true);//hello1003.14true
System.out.println("容量:" + buf1.capacity());//16
System.out.println("长度:" + buf1.length());//5 + 3 = 8 + 4 = 12 + 4 = 16
buf1.append("a");//hello1003.14truea
System.out.println("容量:" + buf1.capacity());//34(原容量 * 2 + 2)
System.out.println("长度:" + buf1.length());//16 + 1 = 17
System.out.println(buf1.toString());//hello1003.14truea
//2.StringBuffer insert(int offset, 任意类型)
StringBuffer buf2 = new StringBuffer();
buf2.append("helloworld");
// buf2.insert(1, "xxx");
// buf2.insert(10, "xxx");//helloworldxxx
// buf2.insert(11, "xxx");//运行时异常: java.lang.StringIndexOutOfBoundsException:offset一定要在 <= length()
System.out.println(buf2);
//3.StringBuffer delete(int start, int end)
StringBuffer buf3 = new StringBuffer();
buf3.append("hello");
// buf3.delete(1, 3);
// buf3.delete(3, 1);//java.lang.StringIndexOutOfBoundsException
// buf3.delete(3, 3);//如果start等于end时,不发生任何更改
buf3.delete(1, 500);//end如果超出范围,不抛异常,会截取到结尾
// buf3.delete(5, 5);//start的范围 < length(),否则运行时异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf3);
//4.StringBuffer deleteCharAt(int index)
StringBuffer buf4 = new StringBuffer();
buf4.append("hello");
buf4.deleteCharAt(1);
buf4.deleteCharAt(5);//索引参数一定要 < length() :运行时异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf4);
//5.StringBuffer replace(int start, int end, String str)
StringBuffer buf5 = new StringBuffer();
buf5.append("hello");
// buf5.replace(1, 3, "ELXXXXXXZ");
//buf5.replace(1, 30, "ELXXXXXXZ");//end超出范围,不抛异常,将截取到字符末尾;
//buf5.replace(1,1, "ELL");//hELLello
//buf5.replace(6, 30, "XXX");//start <= length() 否则异常:java.lang.StringIndexOutOfBoundsException
System.out.println(buf5);
//6.String substring(int start):
StringBuffer buf6 = new StringBuffer();
buf6.append("hello");
String str = buf6.substring(5);
//String str = buf6.substring(6);//start <= length() 否则异常:java.lang.StringIndexOutOfBoundsException
System.out.println("str = " + str);
//7.String substring(int start, int end)
StringBuffer buf7 = new StringBuffer();
buf7.append("hello");
String str2 = buf7.substring(1,3);
/ /String str2 = buf7.substring(5,5);//start,end <= length() 否则异常: java.lang.StringIndexOutOfBoundsException
System.out.println("str2 = " + str2);
//8.StringBuffer reverse()
StringBuffer buf8 = new StringBuffer();
buf8.append("hello");
buf8 = buf8.reverse();
System.out.println("buf8 = " + buf8);
}
}
二、基本数据包装类
基本数据包装类就是把这些基本数据类型封装成一个类。
byteByte
short short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character
short short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character
基本数据类型对象包装类的最常见作用,就是用于基本数据类型和字符串类型之间做转换
字符串转成基本数据类型。
xxx a = Xxx.parseXxx(String);
int a = Integer.parseInt("123");
double b = Double.parseDouble("12.23");
boolean b = Boolean.parseBoolean("true");
Integer i = new Integer("123");
int num = i.intValue();
int a = Integer.parseInt("123");
double b = Double.parseDouble("12.23");
boolean b = Boolean.parseBoolean("true");
Integer i = new Integer("123");
int num = i.intValue();
十进制转成其他进制。
toBinaryString();
toHexString();
toOctalString();
toBinaryString();
toHexString();
toOctalString();
其他进制转成十进制。
parseInt(string,radix);
parseInt(string,radix);
这部分的内容比较多,只要学会使用查阅API,熟悉一些最基本的方法的运用即可。