StringBuilder类
字符串缓冲区,可以提高字符串的操作效率(看成一个长度可以变化的字符串)
底层也是一个数组,但是没有被final修饰,可以改变长度
构造方法:
public StringBuilder ( ) :构造一个空的StringBuilder容器
public StringBuilder (String str ) :构造一个StringBuilder容器,并将字符串添加进去
public class Demo01StrringBuilder {
public static void main(String[] args) {
StringBuilder bu1 = new StringBuilder();
System.out.println("bu1:" + bu1);//bu1:
StringBuilder bu2 = new StringBuilder("abc");
System.out.println("bu2:" + bu2);//bu2:abc
}
}
StringBuilder类的成员方法:
public StringBuilder append (...) :添加任意数据类型的字符串形式,并返回当前对象自身
使用append方法无需接收返回值
链式编程:方法的返回值是一个对象,可以根据对象继续调用方法
public class Demo02StringBuilder {
public static void main(String[] args) {
StringBuilder bu1 = new StringBuilder();
StringBuilder bu2 = bu1.append("abc");
System.out.println(bu1);//abc
System.out.println(bu2);//abc
System.out.println(bu1 == bu2);//true 两个对象是同一个对象
bu1.append("Imagine");
bu1.append(0501);
bu1.append(true);
bu1.append(12.18);
bu1.append("三千");
System.out.println(bu1);//abcImagine0501true12.18三千
//链式编程
StringBuilder bu3 = new StringBuilder();
bu3.append("几秋").append(8.10).append("miss").append("三千");
System.out.println(bu3);//几秋8.1miss三千
}
}
toString方法
StringBuilder和String可以互相转换
String ---> StringBuilder :可以使用StringBuilder的构造方法
StringBuilder (String str) 构造一个字符串生成器,并初始化为指定的字符串内容
StringBuilder --->String :可以使用StringBuilder中的toString方法
public String toString ( ) : 将当前StringBuilder对象转换为String对象
public class Demo02toString {
public static void main(String[] args) {
//String --> StringBuilder
String str = "hello";
System.out.println("str:" + str);
StringBuilder bu = new StringBuilder(str);//str:hello
//往StringBuilder中添加数据
bu.append("world");
System.out.println("bu:" + bu);//bu:helloworld
//StringBuilder --> String
String s = bu.toString();
System.out.println("s:" + s);//s:helloworld
}
}
包装类
装箱:把基本类型的数据,包装到包装类中(基本类型的数据 ---> 包装类)
构造方法:
Integer (int value) :构造一个新分配的 Integer对象,它表示指定的int值
传递的字符串,必须是基本类型的字符串,否则会抛出异常
静态方法:
static Integer valueOf (int i) : 返回一个表示指定的 int 值的Integer实例
stativ Intreger valueOf (String s) : 返回保存指定的String的值的Integer对象
拆箱:在包装类中取出基本类型的数据(包装类 --> 基本类型的数据)
成员方法:
int intValue ( ) : 以 int 类型返回该Integer的值
public class Demo03Integer {
public static void main(String[] args) {
//装箱
Integer in1 = new Integer(1);
System.out.println(in1);//1 重写了toString方法
Integer in2 = new Integer("1");
System.out.println(in2);//1
//静态方法
Integer in3 = Integer.valueOf(1);
System.out.println(in3);//1
Integer in4 = Integer.valueOf("1");
System.out.println(in4);//1
//拆箱
int i = in1.intValue();
System.out.println(i);//1
}
}
自动装箱与自动拆箱:
基本类型的数据和包装类之间可以自动的互相转换
自动装箱:直接把int类型的整数赋值给包装类
自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本类型的数据,再参与计算
public class Demo03Integer2 {
public static void main(String[] args) {
//自动装箱
//Integer in = 1 ; 就相当于 Integer in = new Integer (1);
Integer in = 1;
//自动拆箱
//in + 2;就相当于 in.intValue() + 3 = 3;
//in = in + 2;就相当于 in = new Integer (3)自动装箱
in = in + 2;
System.out.println(in);//3
//ArrayList集合无法直接存储整数,可以存储Integer包装类
ArrayList<Integer> list = new ArrayList<>();
list.add(1);//自动装箱 list.add(new Integer(1));
int a = list.get(0);//自动拆箱 list.get(0).intValue();
}
}
基本类型与字符串之间的转换
基本类型 ---> 字符串
- 基本类型数据的值 + “ ” 最简单的方式
- 使用包装类中的静态方法
- static String toString(int i) 返回一个表示指定整数的String对象
使用String类中的静态方法
static String valueOf (int i) 返回int参数的字符串表示形式
字符串 -----> 基本类型
使用包装类的静态方法parseXX("字符串")
Integer类 : static int parseInt (String s)
Double类 : static double parseDouble (String s)
..........
public class Demo04Integer {
public static void main(String[] args) {
//基本类型 --> 字符串
String s1 = 100 + "";
System.out.println(s1 + 200);//100200
String s2 = Integer.toString(100);
System.out.println(s2 + 200);//100200
String s3 = String.valueOf(100);
System.out.println(s3 + 200);//100200
System.out.println("==========");
//字符串 --> 基本类型
int i = Integer.parseInt("100");
System.out.println(i + 200);//300
}
}