java常用类String类
String类:java程序中的所有字符串字面值如“abc”等,都作为此类的实例实现,时java语言的核心类,提供了字符串的比较、查找、截取、大小写转换等操作。
字符串时常量,它们的值在创建之后不能更改。
String类常见的构造方法
常见的构造方法:
1.public String() 空参构造
2.public String(byte[] bytes) 将byte类型的数组转换成字符串
3.public String(byte[] bytes,int index,int length) 把字节数组的一部分转换成字符串(index:表示的是从第几个索引开始,length:表示的是长度)
4.public String(char[] value) 将字符数组转换成字符串
5.public String(char[] value,int index,int count) 把字符数组一部分转换成字符串
6.public String(String original) 把字符串常量值转换成字符串
public class MyTest{
public static void main(String[] args) {
byte[] bytes=new byte[]{
97,98,99};
String s = new String(bytes);
System.out.println(s);
String s1 = new String(bytes,1,2);
System.out.println(s1);
char[] chars=new char[]{
'我','爱','j','a','v','a'};
String ss = new String(chars);
System.out.println(ss);
String ss1= new String(chars, 3, 3);
System.out.println(ss1);
}
}
结果是
public class MyTest{
public static void main(String[] args) {
String a="abc";
String s = new String(a);
System.out.println(s);
}
}
结果是
字符串常量池
String的特点: 一旦被创建就不能改变,因为字符串的值是在堆内存的常量池中划分空间,分配地址的。
JVM为了提高性能和减少内存的开销,在实例化字符串的时候进行了一些优化:使用字符串常量池。每当创建字符串常量值时,JVM会首先检查字符串常量池,如果该字符串在常量池中已存在,那么就直接返回常量池中的引用。如果该字符不存在,就会实例化该字符串并且将其放到常量池中。由于String字符串不可变性,所以常量池不可能出现两个相同的字符串。
public class MyTest{
public static void main(String[] args) {
String a="hello";
a="world"+"java";
System.out.println(a);
}
}
//打印结果是什么呢?
结果是
原因是"hello"是s的对象实例化,代码运行到String a="hello"这行代码时,JVM会先在字符常量池中找有没有"hello",结果没有,就在字符常量池中对"hello"进行实例化,然后将地址赋值给a,运行到a="world"+"java"时,JVM还是会去字符常量池中找"world",结果没有,java中+运算符会进行优化,所以JVM就会将"worldjava"在字符常量池中进行实例化并分配地址,将地址赋值给a,所以a打印出来的结果就是"worldjava"。
equals方法
equals方法在String类重写,比较的时两个字符串内容是否相同。
==比较的是对象的地址是否相同。
public class MyTest4 {
public static void main(String[] args) {
String s = new String("abc"); //这里在堆内存中创建两个对象
String s1="abc";
System.out.println(s == s1);
System.out.println(s.equals(s1));
}
}
结果是
String s=new String("abc")会在堆内存中创建两个对象?
原因就是"abc"是字面值常量,他会先在堆内存中的字符串常量池中初始化分配空间,然后通过new关键字在堆内存中又创建一个对象,而这个对象里面存放的就是"abc"在字符串常量池的地址,再将new出来这个对象的地址传给s,所以s和s1的地址不相同
String类的判断方法
public boolean equals(Object obj); 判断字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str); 判断字符串的内容是否相同,不区分大小写
public boolean contains(String str); 判断字符串是否包含传递的进来字符串
public boolean startswith(String str); 判断字符串是否是以传递进来的字符串开头
public boolean endswith(String str); 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(); 判断字符串的内容是否为空串
public class MyTest5 {
public static void main(String[] args) {
String a="abc";
boolean a1=a.equals("ABC");
System.out.println(a1);
boolean a2=a.equalsIgnoreCase("ABC");
System.out.println(a2);
String s="爱生活,爱java";
System.out.println(s.contains("java"));
System.out.println(s.startsWith("爱"));
System.out.println(s.endsWith("java"));
}
}
结果是
String类的获取功能
public int length(); 获取字符串长度
public char charAt(int index); 获取指定索引位置的字符
public int indexOf(int ch); 返回指定字符在此字符串第一次出现处的索引
public int indexOf(String str); 返回指定字符串在此字符串第一次出现处的索引
public int indexOf(int ch,int fromIndex); 返回指定字符在此字符串中从指定位置后的第一次出现处的索引位置
public int indexOf(String str,int fromIndex); 返回指定字符串在此字符串中从指定位置后的第一次出现处的索引位置
public String substring(int start); 从指定位置开始截取字符串,默认到结尾
public String substring(int start,int end); 从指定位置开始到指定位置结束截取字符串
String类的转换功能
public byte[] getBytes(); 将字符串转换为字节数组
public char[] toCharArray(); 把字符串转换为字符数组
public static String valueOf(char[] chs); 将字符数组转换为字符串
public static String valueOf(int i); 将int类型的数据转换成字符串类型
valueOf()方法可以将任意类型的数据转换成字符串类型
public String toLowerCase(); 将字符串转换成小写
public String toUpperCase(); 将字符串转换成大写
public String concat(String str); 字符串拼接