一. String类
- Sring类:代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作
为此类的实例实现。 - String是一个final类,代表不可变的字符序列
- 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
- 当对字符串重写赋值时,需要重写指定内存区域值,不能使用原有的value进行赋值
- 当堆字符串修改时,也需要重写指定内存区域值,不能使用原有的value进行赋值
- String对象的字符内容是存储在一个字符数组value[]中的
- 通过字面量的方式(区别于new)給一个字符串赋值,此时的字符串置声明在在字符串常量池中
- 字符串常量池中是不会存储相同内容的字符串的.
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
- 常用方式
// 1.通过new +构造器的方式,此时s1和s2保存的地址值,是数据在堆空间中开辟空降以后对应的地址值
String a = new String("abc");
String b = new String("abc");
System.out.println(a == b);
// 2.字面量定义,此时a和b 在字符串常量池中
String c = "abc";
String d = "abc";
System.out.println(c == d);
a = "hello"; // 此时的地址变了
String对象的创建
String str = "hello";
//本质上this.value = new char[0];
String s1 = new String();
//this.value = original.value;
String s2 = new String(String original);
//this.value = Arrays.copyOf(value, value.length);
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count);
字符串对象是如何存储的
- 面试题: String s =new String("abc);方式创建的duixiang,在内存中创建了几个对象
- 答:一个是堆空间中new结构,另一个是char[]对应的常量池中数据 : "abc
二.StringBuffer和StringBuilder
- String和StringBuffer和StringBuilder的异同
- String:不可变的字符序列,底层结构使用char[]数组
- StringBuffer:可变的字符序列->线程安全的:效率低(同步方法),底层结构使用char[]数组
- StringBuilder:可变的字符序列->Jdk1.5新增,线程不安全,效率高,底层结构使用char[]数组
// 源码分析
String str = new String();// char[] value=new char[0]
String str1 = new String("adc");// char[] value=new char[]{'a','b','c'}
StringBuffer sb1 = new StringBuffer();//char[] value=new char[16] 底层创建了一个长度为16的char数组
sb1.append('a');//value[0]='a'
sb1.append('b');// value[1]='b'
StringBuffer sb2 = new StringBuffer("abc");//char[] value=new char["abc".length()+16]
System.out.println(sb1.length()); //2
System.out.println(sb2.length()); //3
//扩容:原来数量的2倍+2,同时将原有数据中的元素复制到新的数组中
// 建议:开发中建议大家使用 :StringBuffer(int capacity)
- 效率对比
- StringBuilder>StringBuffer>String
public static void main(String[] args) {
//初始设置
long startTime = 0L;
long endTime = 0L;
String text = "";
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
//开始对比
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String的执行时间:" + (endTime - startTime));
}
StringBuffer的执行时间:7
StringBuilder的执行时间:4
String的执行时间:1947
三. 日期时间
Jdk1.8之前
Jdk1.8之后(推荐使用)
四.Java 比较器
五.System类
六.Math类
七.BigInteger与BigDecimal