学习目标:
Java String操作及基础知识学习内容:
1、 理解String不可变性 2、 理解String不同实例方式的对比 3、 掌握String不同拼接操作的对比 4 了解String常用方法学习时间:
2021/1/20学习产出:
一, String不可变性以及不同实例拼接操作
1.常量与常量的拼接结果在常量池中,且常量词中不会存在相同的常量。
2.只要其中有一个是变量,结果就在堆中。
String:字符串,使用一对“”引起来表示
1.String声明final的,不可被继承
2.String实现了Serializable接口:表示字符串支持可序列化的
实现了Comparable接口:表示String可以比较大小
3.String内部定义了final char[] value用于储存字符串数据
4.String:代表不可变的字符串序列。简称不可变
体现:1.当对字符串重新赋值时,需要重新指定内存储存值,不会使用原有的value赋值。
2.当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能对原有的值进行改变
3.当使用String的replace()方法修改字符或字符串时,也需要重新开辟地址进行赋值
5.通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明再字符串常量池中。
6.字符串常量池中不会储存相同内容的字符串
部分代码展示
public void test3(){
String s1 = "Java";
String s2 = "hadoop";
String s3 = "JavaEEhadoop";
String s4 = "JavaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "JavaEE" + s2;
String s7 = s1 + s2;
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(s6 == s7);//false
String s8 = s5.intern();//返回常量池已经存在的常量
System.out.println(s3 == s8);
}
public void test2(){
//此时s1和s2声明在方法区的字符串常量池中
String s1 = "JavaEE";
String s2 = "JavaEE";
//此时的s3和s4保存值,是先在堆空间开辟空间以后进行赋值
String s3 = new String("JavaEE");
String s4 = new String("JavaEE");
System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//false
System.out.println(s3 == s4);//false
Persion p1 = new Persion("Tom",18);
Persion p2 = new Persion("Tom",18);
System.out.println(p1.name.equals(p2.name));//true
System.out.println(p1.name == p2.name);
p2.name = "Jerry";
System.out.println(p1);
}
public void text1(){
String s1 = "abc"; //字面值
String s2 = "abc";
System.out.println(s1 == s2); //比较s1和s2的地址值
System.out.println(s1);
System.out.println(s2);
System.out.println("*************************");
String s3 = "abc";
s3+="def";
System.out.println(s3);//abcdef
System.out.println(s2);
System.out.println("**************************");
String s4 = "abc";
String s5 = s4.replace("a","m");
System.out.println(s4);
System.out.println(s5);
}
二,Java String面试题
public class ExerStringTest {
String str = new String("good");
char[] ch = {'t','e','s','t'};
public void change(String str,char ch[]){
str = "test ok";
ch[0] = 'b';
}
public static void main(String[] args) {
ExerStringTest ex = new ExerStringTest();
ex.change(ex.str,ex.ch);
System.out.println(ex.str + " and");//good and
System.out.println(ex.ch);//best
}
}
String常用方法的例子
public class StringMethodTest {
@Test
public void test4(){
String str = "北京尚硅谷";
String r1 = str.replace("北", "东");//替换一个字符
System.out.println(r1);
String r2 = str.replace("北京", "上海");//替换字符串
System.out.println(r2);
}
@Test
public void test3(){
String str1 = "helloworld";
boolean b1 = str1.endsWith("ld");//检验是否以指定字符串结束
System.out.println(b1);
boolean b2 = str1.startsWith("he");//检验是否以指定字符串开始
System.out.println(b2);
boolean b3 = str1.startsWith("ll",2);
System.out.println(b3);
String str2 = "wor";
System.out.println(str1.contains(str2));//检验是否包含
System.out.println(str1.indexOf("lo"));//返回字符串第一次出现的位置
System.out.println(str1.indexOf("lo",5));//返回字符串第一次出现的位置,每次叠加
//str1.lastIndexOf()从后面开始找
//什么情况下,indexof(str)和lastindex(str)返回值相同?
//情况一:存在唯一一个str
}
@Test
public void test2(){
String s1 = "HelloWorld";
String s2 = "helloworld";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s3 = "abc";
String s4 = s3.concat("def");
System.out.println(s4);
String s5 = "abc";
String s6 = new String("abd");
System.out.println(s5.compareTo(s6));//涉及到字符串排序
String s7 = "北京尚硅谷教育";
String s8 = s7.substring(2);
System.out.println(s7);
System.out.println(s8);
String s9 = s7.substring(2,5);//左闭右开
}
@Test
public void test1(){
String s1 = "helloworld";
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(9));
//str = "";
System.out.println(s1.isEmpty());
String s2 = s1.toLowerCase();
System.out.println(s1);//s1不可变
System.out.println(s2);//s2改成小写
String s3 = " hel lo wo r l d";
String s4 = s3.trim();
System.out.println("----"+s3+"-----");
System.out.println("-----"+s4+"-----");
}
}