黑马程序员——StringBuffer&StringBuilder&Integer
2013-05-30 21:13
381 查看
------- android培训、java培训、期待与您交流! ----------
StringBuffer: 同步的
StringBuilder: 非同步的,
执行速度比Stringbufferkuai,单线程程序建议使用StringBuilder
String是一个常量
StringBuffer是一个字符串缓冲区,
底层是一个数组, 它将所有的字符存进了这个数组
打印StringBuffer对象时其实是打印的是对象.toString()
StringBuffer也有获得字符串长度的方法 length()
2. StringBuffer构造方法
2.1 public StringBuffer()
构造一个默认长度为16个字符的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer();
2.2 StringBuffer(int len)
构造一个长度为len个字符的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer(5);
2.3 StringBuffer(String s)
构造一个指定内容的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer(“Ab12”);
3. StringBuffer常用成员方法
3.1 append方法(重点)
声明: public StringBuffer append(参数)
这个方法是StringBuffer()类里最常用的方法,
用于向StringBuffer上添加内容
这个方法的参数可以是基本类型, char[], String类型和Object对象等,
会将这些传入的参数以字符串的形式追加到StringBuffer对象的末尾处
示例:
classStringBuff {
public static void main(String[] args) {
StringBuffer buff = newStringBuffer();
buff.append(123).append(true).append("Abc").append('+');
System.out.println(buff);
}
}
打印结果是: 123trueAbc+
3.2 insert方法
声明: public StringBuffer insert(参数)
将指定的数据插入指定的位置,
可以接受char[], 也可以指定插入char[]
中第几位开始的几个字符
示例:
classInsertTest {
public static void main(String[] args) {
//创建一个StringBuffer对象
StringBuffer buff = newStringBuffer();
//往StringBuffer对象添加数据
buff.append("abcd");
//添加int类型的数据添加到buff中的指定位置
int i = 100;
buff.insert(2, i);
//打印结果
System.out.println(buff); //打印结果: ab100cd
//添加char[]中的字符插入到buff中的指定的位置
/* char[] ch = {'H', 'e', 'l','l', 'o'};
buff.insert(2, ch);
//打印结果
System.out.println(buff); //打印结果: abHellocd
*/
//添加char[]中指定位置开始的几个字符插入到buff中指定的位置
/* char[] ch = {'n', 'i', 'h','a', 'o'};
buff.insert(2, ch, 2, 3);
System.out.println(buff); //打印结果: abhaocd
*/
}
}
3.3 delete方法
3.3.1 声明: public StringBuffer delete(int start, int end)
删除字符串中start到end之间的字符
3.3.2 声明: public StringBuffer deleteCharAt(int index)
删除字符串中指定位置的字符
示例:
class DeleteTest {
publicstatic void main(String[] args) {
//创建并将字符串赋给StringBuffer对象
StringBufferbuff = new StringBuffer("HelloWord");
//删除从第5个开始到结束的字符串
// buff.delete(5,buff.length());
// System.out.println(buff); //打印结果: Hello
//删除buff中下标为0的字符
buff.deleteCharAt(0);
System.out.println(buff); //打印结果: elloWord
}
}
3.4 reverse方法
声明: publicStringBuffer reverse()
作用: 将字符串反转
示例:
class ReverseTest {
public static voidmain(String[] args) {
//创建StringBuffer对象
StringBufferbuff = new StringBuffer("Hello World!");
//调用reverse方法
buff.reverse();
System.out.println(buff); //打印结果: !dlroW olleH
}
}
3.5 setCharAt方法
声明: public voidsetCharAt(int index, char ch)
作用: 将指定位置的字符替换成传入的新字符
示例:
class SetCharAtTest {
public static voidmain(String[] args) {
StringBufferbuff = new StringBuffer("Hello World!");
//将下标为0的字符替换为A
buff.setCharAt(0,'A');
System.out.println(buff); //打印结果: Aello World!
}
}
他们的区别是StringBuilder是同步的
基本类型数据作为一个包装类的属性, 实现基本数据类型数据与引用类型数据(String)之间的转换
2. 基本数据类型的包装类
整型:
byte --------------------------------à Byte
short --------------------------------à Short
int --------------------------------à Integer
long --------------------------------à Long
浮点型
float --------------------------------à Float
double --------------------------------à Double
字符型
char --------------------------------à Character
布尔型
boolean --------------------------------à Boolean
a) public Integer(int i)
构造一个新分配的int对象,它表示指定的int值
示例:
Integer in = new Integer(10);
b) public Integer(String s)
构造一个新分配的String对象,它表示String参数所指示的int值
示例
Integer in = new Integer(“123”);
2. Integer类中常用方法
a) intValue()
声明: public int intValue()
作用: 得到Integer对象中的value属性
示例:
classIntegerTest {
public static void main(String[] args) {
//创建一个Integer对象
Integer in = new Integer(10);
//用intValue()将Integer对象的value属性返回
int i = in.intValue();
System.out.println(i); //打印结果: 10
}
}
b) toString方法, parseInt方法
4.2.1声明: public String toString()
作用: 将Integer类型数据转换为String类型
4.2.2 声明: public Integer parseInt()
作用: 将String类型数据转换为int类型
示例:
class Integer2String {
public static voidmain(String[] args) {
//创建Integer对象
Integer in1 = newInteger(10);
//调用toString()方法将Integer类型数据转换为String
String s1 =in1.toString();
//将转换后的数据加1
结果不是相加而是拼接
System.out.println(s1+ 1); //打印结果: 101
a7fc
//创建一个全部是数字字符的String对象
String s2 ="10";
//调用parseInt方法将字符串转换为Integer类型
Integer in2 =Integer.parseInt(s2);
//将转换后的数据加1
结果是相加而不是拼接
System.out.println(in2+ 1); //打印结果: 11
}
}
c) 自动装箱,
自动拆箱
自动装箱时将int类型的数据直接包装为Integer
自动拆箱是将Integer类型的数据直接转换为int
示例:
Integer i = 10; 装箱
Int a = i;
拆箱
3. 关于Integer的笔试题:
Integer in1 = newInteger(1000);
Integer in2 = newInteger(1000);
Integer in3 = 128;
Integer in4 = 128;
in1 == in2, in3 ==in4分别会返回什么
答案是false, false
Integer in1 = 127;
Integer in2 = 127;
in1 == in2 返回什么 //返回true
原因是Integer在底层维护了一个byte类型的数据,
直接将数值范围在byte类型内赋给Integer类型的引用时的,Integer将不会创建新的对象,
直接将已有的值指向Integer对象, byte范围在-128~127,
所以上面的两个比较都是false, 下面是true
StringBuffer类, StringBuilder类,包装类
StringBuffer与Stringbuilder区别:StringBuffer: 同步的
StringBuilder: 非同步的,
执行速度比Stringbufferkuai,单线程程序建议使用StringBuilder
StringBuffer
1. StringBuffer与String区别String是一个常量
StringBuffer是一个字符串缓冲区,
底层是一个数组, 它将所有的字符存进了这个数组
打印StringBuffer对象时其实是打印的是对象.toString()
StringBuffer也有获得字符串长度的方法 length()
2. StringBuffer构造方法
2.1 public StringBuffer()
构造一个默认长度为16个字符的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer();
2.2 StringBuffer(int len)
构造一个长度为len个字符的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer(5);
2.3 StringBuffer(String s)
构造一个指定内容的字符串缓冲区
示例:
StringBuffer buff = new StringBuffer(“Ab12”);
3. StringBuffer常用成员方法
3.1 append方法(重点)
声明: public StringBuffer append(参数)
这个方法是StringBuffer()类里最常用的方法,
用于向StringBuffer上添加内容
这个方法的参数可以是基本类型, char[], String类型和Object对象等,
会将这些传入的参数以字符串的形式追加到StringBuffer对象的末尾处
示例:
classStringBuff {
public static void main(String[] args) {
StringBuffer buff = newStringBuffer();
buff.append(123).append(true).append("Abc").append('+');
System.out.println(buff);
}
}
打印结果是: 123trueAbc+
3.2 insert方法
声明: public StringBuffer insert(参数)
将指定的数据插入指定的位置,
可以接受char[], 也可以指定插入char[]
中第几位开始的几个字符
示例:
classInsertTest {
public static void main(String[] args) {
//创建一个StringBuffer对象
StringBuffer buff = newStringBuffer();
//往StringBuffer对象添加数据
buff.append("abcd");
//添加int类型的数据添加到buff中的指定位置
int i = 100;
buff.insert(2, i);
//打印结果
System.out.println(buff); //打印结果: ab100cd
//添加char[]中的字符插入到buff中的指定的位置
/* char[] ch = {'H', 'e', 'l','l', 'o'};
buff.insert(2, ch);
//打印结果
System.out.println(buff); //打印结果: abHellocd
*/
//添加char[]中指定位置开始的几个字符插入到buff中指定的位置
/* char[] ch = {'n', 'i', 'h','a', 'o'};
buff.insert(2, ch, 2, 3);
System.out.println(buff); //打印结果: abhaocd
*/
}
}
3.3 delete方法
3.3.1 声明: public StringBuffer delete(int start, int end)
删除字符串中start到end之间的字符
3.3.2 声明: public StringBuffer deleteCharAt(int index)
删除字符串中指定位置的字符
示例:
class DeleteTest {
publicstatic void main(String[] args) {
//创建并将字符串赋给StringBuffer对象
StringBufferbuff = new StringBuffer("HelloWord");
//删除从第5个开始到结束的字符串
// buff.delete(5,buff.length());
// System.out.println(buff); //打印结果: Hello
//删除buff中下标为0的字符
buff.deleteCharAt(0);
System.out.println(buff); //打印结果: elloWord
}
}
3.4 reverse方法
声明: publicStringBuffer reverse()
作用: 将字符串反转
示例:
class ReverseTest {
public static voidmain(String[] args) {
//创建StringBuffer对象
StringBufferbuff = new StringBuffer("Hello World!");
//调用reverse方法
buff.reverse();
System.out.println(buff); //打印结果: !dlroW olleH
}
}
3.5 setCharAt方法
声明: public voidsetCharAt(int index, char ch)
作用: 将指定位置的字符替换成传入的新字符
示例:
class SetCharAtTest {
public static voidmain(String[] args) {
StringBufferbuff = new StringBuffer("Hello World!");
//将下标为0的字符替换为A
buff.setCharAt(0,'A');
System.out.println(buff); //打印结果: Aello World!
}
}
StringBuilder
1. StringBuilder构造方法与方法同StringBuffer一样,他们的区别是StringBuilder是同步的
基本数据类型的包装类
1. 包装类就是将基本数据类型包装成一个类,基本类型数据作为一个包装类的属性, 实现基本数据类型数据与引用类型数据(String)之间的转换
2. 基本数据类型的包装类
整型:
byte --------------------------------à Byte
short --------------------------------à Short
int --------------------------------à Integer
long --------------------------------à Long
浮点型
float --------------------------------à Float
double --------------------------------à Double
字符型
char --------------------------------à Character
布尔型
boolean --------------------------------à Boolean
Integer类
1. Integer类中构造方法a) public Integer(int i)
构造一个新分配的int对象,它表示指定的int值
示例:
Integer in = new Integer(10);
b) public Integer(String s)
构造一个新分配的String对象,它表示String参数所指示的int值
示例
Integer in = new Integer(“123”);
2. Integer类中常用方法
a) intValue()
声明: public int intValue()
作用: 得到Integer对象中的value属性
示例:
classIntegerTest {
public static void main(String[] args) {
//创建一个Integer对象
Integer in = new Integer(10);
//用intValue()将Integer对象的value属性返回
int i = in.intValue();
System.out.println(i); //打印结果: 10
}
}
b) toString方法, parseInt方法
4.2.1声明: public String toString()
作用: 将Integer类型数据转换为String类型
4.2.2 声明: public Integer parseInt()
作用: 将String类型数据转换为int类型
示例:
class Integer2String {
public static voidmain(String[] args) {
//创建Integer对象
Integer in1 = newInteger(10);
//调用toString()方法将Integer类型数据转换为String
String s1 =in1.toString();
//将转换后的数据加1
结果不是相加而是拼接
System.out.println(s1+ 1); //打印结果: 101
a7fc
//创建一个全部是数字字符的String对象
String s2 ="10";
//调用parseInt方法将字符串转换为Integer类型
Integer in2 =Integer.parseInt(s2);
//将转换后的数据加1
结果是相加而不是拼接
System.out.println(in2+ 1); //打印结果: 11
}
}
c) 自动装箱,
自动拆箱
自动装箱时将int类型的数据直接包装为Integer
自动拆箱是将Integer类型的数据直接转换为int
示例:
Integer i = 10; 装箱
Int a = i;
拆箱
3. 关于Integer的笔试题:
Integer in1 = newInteger(1000);
Integer in2 = newInteger(1000);
Integer in3 = 128;
Integer in4 = 128;
in1 == in2, in3 ==in4分别会返回什么
答案是false, false
Integer in1 = 127;
Integer in2 = 127;
in1 == in2 返回什么 //返回true
原因是Integer在底层维护了一个byte类型的数据,
直接将数值范围在byte类型内赋给Integer类型的引用时的,Integer将不会创建新的对象,
直接将已有的值指向Integer对象, byte范围在-128~127,
所以上面的两个比较都是false, 下面是true
相关文章推荐
- 黑马程序员—Java基础学习笔记之String&&StringBuffer&&StringBuilder
- 黑马程序员_毕向东Java基础_集合(1)String&StringBuffer&StringBuilder&基本数据类型包装类
- 黑马程序员 知识点总结-Java String&StringBuffer&StringBuilder
- 黑马程序员——基础学习(九)String的实际案例及StringBuffer/StringBuilder
- 黑马程序员-------String StringBuffer StringBuilder
- 黑马程序员-第十二天( StringBuffer/StringBuilder)
- 黑马程序员---易错、易望、基础、重要知识点 ——API常用类1——StringBuffer/StringBuilder
- Overloading '+' VS. StringBuilder and StringBuffer
- StringBuilder&StringBuffer&String
- java之String & StringBuffer & StringBuilder
- 黑马程序员_StringBuffer StringBuilder
- 黑马程序员 java 基础 毕向东 面向对象 String StringBuffer StringBuilder 基本数据类型包装类
- 黑马程序员——java-几个常用的类(Object,Scanner,String,StringBuffer/StringBuilder)
- StringBuffer & StringBuilder & String,常用类
- 黑马程序员___StringBuilder&正则表达式 &Interger
- 黑马程序员_常用API(1) String&StringBuffer&StringBuilder
- 性能优化:StringBuilder>StringBuffer>String
- 黑马程序员_Java学习日记_JAVA中API中对象String和StringBuffer/StringBuilder
- String StringBuffer StringBuilder 三者的区别
- String/StringBuffer/StringBuilder学习记录