Java常用类总结
字符串相关的类
String类
String:字符串,使用一对""引起来表示
-
String声明为final的,不可被继承
-
Sting实现了Serializable接口:表示字符串是支持序列化的
实现了Comparable接口:表示String可以比较大小
-
String内部定义了final char[] value用于存储字符串数据
-
String:代表不可变的字符序列。简称:不可变性
体现:
1.当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值
2.当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。字符串常量池中是不会存储相同内容的字符串的。
3.当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值
- 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中
- 字符串常量池中是不会存储相同内容的字符串的。
public class StringTest {
@Test
public void test1() {
String s1 = "abc"; //字面量的定义方式
String s2 = "abc";
s1 = "hello";
System.out.println(s1 == s2); //比较s1和s2地址值是否相同,false
System.out.println(s1); //hello
System.out.println(s2); //abc
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); //abc
System.out.println(s5); //mbc
}
}
String 的实例化方式:
方式一:通过字面量定义的方式
方式二:通过new+构造器的方式
String str=“hello”;
String s1=new String(); //本质上 this.value=new char[0];
String s2=new String(String original); //本质上this.value=original.value;
String s3=new String(char[] a); //本质上this.value=Arrays.copyOf(value,value.length);
String s4=new String(char[] a,int startIndex,int count);
String不同实例化方式的对比
@Test
public void test2(){
//通过字面量定义的方式,此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
String s1="javaEE";
String s2="javaEE";
//通过new+构造器的方式:此时的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
}
String不同拼接操作的对比
@Test
public void test3(){
String s1="javaEE";
String s2="haddoop";
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(s5==s6);//false
System.out.println(s5==s7);//false
System.out.println(s6==s7);//false
String s8=s5.intern(); //返回值得到的s8使用的是常量池中已经存在的“javaEEhadoop"
System.out.println(s8==s3);
}
-
常量与常量的拼接结果在常量池中,且常量池中不会存在相同内容的常量
-
只要其中有一个是常量,结果就在堆中
-
如果拼接的结果调用intern()方法,返回值就在常量池中
String常用方法
一、
int length():返回字符串的长度: return value.length
char charAt(int index): 返回某索引处的字符return value[index]
boolean isEmpty():判断是否是空字符串:return value.length == 0
String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写
String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写
String trim():返回字符串的副本,忽略前导空白和尾部空白
boolean equals(Object obj):比较字符串的内容是否相同
boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+”
int compareTo(String anotherString):比较两个字符串的大小
String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
@Test
public void test1(){
//单元测试方法
String s1="HelloWorld";
System.out.println(s1.length()); //10
System.out.println(s1.charAt(0)); //H
System.out.println(s1.charAt(9)); //d
// System.out.println(s1.charAt(10)); //-1
// s1="";
System.out.println(s1.isEmpty());
String s2=s1.toLowerCase();
System.out.println(s1); //s1不可变的,仍然为原来的字符串
System.out.println(s2); //改成小写以后的字符串
String s3=" he llo world ";
String s4=s3.trim();
System.out.println("--------"+s3+"--------");
System.out.println("--------"+s4+"---------");
}
@Test
public void test2(){
String s1="HelloWorld";
String s2="helloworld";
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true
String s3="abc";
String s4=s3.concat("defg");
System.out.println(s4); //abcdefg
String s5="abc";
String s6=new String("abg");
System.out.println(s5.compareTo(s6)); //涉及字符串排序,-4
String s7="北京尚硅谷教育";
String s8=s7.substring(2);
System.out.println(s7);
System.out.println(s8);//尚硅谷教育
String s9=s7.substring(2,5);
System.out.println