通过字面量方式给字符串赋值,此时字符串声明在字符串常量池中,在字符串常量池中不允许存储相同内容的字符串。
方法一:通过字面量方式,将字符串声明在字符串常量池中
方式二:通过new + 构造器方式,保存的是地址值,是数据在堆空间中开辟空间以后对应地址值。
public void test(){
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s1 == s2);//true
System.out.println(s3 == s4);//flase
}
String s = new String(“abc”);方法创建对象,在内存中创建了几个对象?
两个:一个是在堆空间中new,另一个是char[]对应的常量池数据:“abc”
public void test(){
String s1 = "java";
String s2 = "ee";
String s3 = "javaee";
String s4 = "java" + "ee";
String s5 = s1 + "ee";
String s6 = "java" + s2;
String s7 = s1 + s2;
String s8 = s5.intern();
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s3 == s8);//true
}
public class test {
String str = "good";
public void change(String str){
str = "test";
}
public static void main(String[] args) {
test test = new test();
test.change(test.str);
System.out.println(test.str);//good
}
}
public void test(){
String s1 = "java";
String s2 = " java ";
String s3 = "JAVa";
System.out.println(s1.length());//4
System.out.println(s2.trim());//java
System.out.println(s1.charAt(1));//a
System.out.println(s3.toUpperCase());//JAVA
System.out.println(s3.toLowerCase());//java
System.out.println(s3.equals(s1));//false
System.out.println(s3.equalsIgnoreCase(s1));//true
System.out.println(s1.concat("ee"));//javaee
System.out.println(s1.substring(2));//va
System.out.println(s1.substring(2,3));//v
System.out.println(s3.compareTo(s1));//-32
}
boolean endsWith(String sufflx):测试字符串是否以指定的后缀结束
boolean startsWith(String preflx):测试字符串是否以指定的前缀开始
boolean startsWith(String preflx,Int toffset):测试此字符串是否从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定char序列返回true
int lastIndexOf(String str):返回指定字符串在此字符串中最右边出现的索引
int lastIndexOf(String str,int fromIndex):返回指定子字符在此字符串中最后一次出现的索引,从指定索引开始反向查找
int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现的索引,从指定索引开始。
indexOf和lastIndexOf如果未找到都是返回-1
public void test(){
String s1 = "helloworld";
System.out.println(s1.endsWith("d"));//true
System.out.println(s1.endsWith("ld"));//true
System.out.println(s1.startsWith("h"));//true
System.out.println(s1.startsWith("e",1));//true
System.out.println(s1.contains("world"));//true
System.out.println(s1.lastIndexOf("o"));//6
System.out.println(s1.lastIndexOf("o",5));//4
System.out.println(s1.indexOf("o"));//4
System.out.println(s1.indexOf("o",5));//6
System.out.println(s1.indexOf("o",7));//-1
System.out.println(s1.lastIndexOf("o",3));//-1
}
public void test(){
String s1 = "123";
int i = Integer.parseInt(s1);
String s2 = String.valueOf(i);
String s3 = i +"";
}
public void test(){
String s1 = "hello";
char[] chars = s1.toCharArray();
for (char c : chars) {
System.out.println(c);
}
String s2 = new String(chars);
System.out.println(s2);
}
线程安全,效率低;底层使用可变的char[]
底层数组大小默认16
/** StringBuffer.java**/
public StringBuffer() {
super(16);
}
/**AbstractStringBuilder.java**/
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
length()返回依旧是添加的内容长度
public void test(){
StringBuffer stringBuffer = new StringBuffer();
System.out.println(stringBuffer.length());//0
}
public synchronized int length() {
return this.count;
}
如果添加的数据底层数组装不下,那就需要扩容底层数组,默认情况下,扩容为原来容量的2倍+2,同时将原有数组中的元素复制到新的数组中。
append(xxx):用于进行字符串拼接
delete(int start ,int end):删除指定位置的内容
replace(int start ,int end ,String str):把[start,end]位置替换为str
insert(int offset,xxx):在指定位置插入xxx
reverse():把当前字符序列逆转
public void test(){
StringBuffer stringBuffer = new StringBuffer("abcdef");
stringBuffer.append("hello");
System.out.println(stringBuffer);//abcdefhello
stringBuffer.replace(0,2,"java");
System.out.println(stringBuffer);//javacdefhello
stringBuffer.insert(5,"hi");
System.out.println(stringBuffer);//javachidefhello
stringBuffer.delete(5,7);
System.out.println(stringBuffer);//javacdefhello
stringBuffer.replace(0,4,"ab");
System.out.println(stringBuffer);//abcdefhello
stringBuffer.reverse();
System.out.println(stringBuffer);//ollehfedcba
}
线程不安全,效率高;底层使用可变的char[]
同StringBuffer;