1.什么是字符串
字符序列:把多个字符按照一定得顺序排列起来.字符串:把多个字符串串联起来(好比羊肉串).
字符串的分类:不可变的字符串:String:当前对象创建完毕之后,该对象的内容(字符序列)是不能改变的,一旦内容改变就是一个新的对象.
可变的字符串:StringBuilder/StringBuffer:当对象创建完毕之后,该对象的内容可以发生改变,当内容发生改变的时候,对象保持不变.
字符串的本质(底层是什么其实就是char[]),char表示一个字符,数组表示同一种类型的多个数据如何理解char[]:
String str = “ABCDEFG”; //定义一个字符串对象,等价于
char[] cs = new char[]{'A','B','C','D','E','F','G'};
2.String类
String类:表示不可变的字符串,当前对象创建完毕之后,该对象的内容(字符序列)是不能改变的,一旦内容改变就是一个新的对象.
-------------------------------------------------------------------------------
String对象的创建:
1):直接赋一个字面量: String str1 = “ABCD”;
2):通过构造器创建: String str2 = new String(“ABCD”);
-------------------------------------------------------------------------------
String对象的创建:
1):直接赋一个字面量: String str1 = “ABCD”;
2):通过构造器创建: String str2 = new String(“ABCD”);
两种方式有什么区别,分别在内存中如何分布?
String str1 = “ABCD”;
最多创建一个String对象,最少不创建String对象.
如果常量池中,已经存在”ABCD”,那么str1直接引用,此时不创建String对象.
否则,先在常量池先创建”ABCD”内存空间,再引用.
String str2 = new String(“ABCD”);最多创建两个String对象,至少创建一个String对象.
new关键字:绝对会在堆空间,创建内存区域. 所以至少创建一个String个对象.
如果常量池中,已经存在”ABCD”,那么str1直接引用,此时不创建String对象.
否则,先在常量池先创建”ABCD”内存空间,再引用.
String str2 = new String(“ABCD”);最多创建两个String对象,至少创建一个String对象.
new关键字:绝对会在堆空间,创建内存区域. 所以至少创建一个String个对象.
String对象的空值:
1):表示引用为空(null): String str 1 = null; 没有初始化,没有分配内存空间.
2):内容为空字符串: String str2 = “”; 已经初始化,分配内存空间,不过没有内容.
--------------------------------------------------------------------------------
判断字符串非空:
1):引用不能为空(null);2):字符内容不能为空字符串(“”);
--------------------------------------------------------------------------------
字符串的比较操作:
1):使用”==”号: 只能比较引用的内存地址是否相同.
2):使用equals方法: 在Object类中和”==”号相同,建议子类覆盖equals方法去比较自己的内容.
String类覆盖equals方法,比较的是字符内容.
1):表示引用为空(null): String str 1 = null; 没有初始化,没有分配内存空间.
2):内容为空字符串: String str2 = “”; 已经初始化,分配内存空间,不过没有内容.
--------------------------------------------------------------------------------
判断字符串非空:
1):引用不能为空(null);2):字符内容不能为空字符串(“”);
--------------------------------------------------------------------------------
字符串的比较操作:
1):使用”==”号: 只能比较引用的内存地址是否相同.
2):使用equals方法: 在Object类中和”==”号相同,建议子类覆盖equals方法去比较自己的内容.
String类覆盖equals方法,比较的是字符内容.
字符串equals方法的源码
覆盖了Object的equals方法,比较的是字符串中的内容.
3.String对象比较
public class StringDemo1 {
private static String getXx() {
return "AB";
}
public static void main(String[] args) {
String str1 = "ABCD";
String str2 = "A" + "B" + "C" + "D";
String str3 = "AB" + "CD";
String str4 = new String("ABCD");
String temp = "AB";
String str5 = temp + "CD";
String str6 = getXx() + "CD";
System.out.println(str1==str2);//true
System.out.println(str1==str3);//true
System.out.println(str1==str4);//false
System.out.println(str1==str5);//false
System.out.println(str1==str6);//false
}
}
通过反编译工具查看:2):使用new String("")创建的对象会存储到堆内存中,是运行期才创建;
3):使用只包含直接量的字符串连接符如"aa" + "bb"创建的也是直接量编译期就能确定,已经确定存储到常量池中(str2和str3),存在编译器优化;
4):使用包含String直接量(无final修饰符)的字符串表达式(如"aa" + s1)创建的对象是运行期才创建的,存储在堆中;
通过变量/调用方法去连接字符串,都只能在运行时期才能确定变量的值和方法的返回值,不存在编译优化操作.str5和str6都是运行时才使用new 关键字创建对象,所以跟s1比较肯定不会相等.
4.String类中的常用方法(从API中去找下列方法,并且读)
1):String的创建和转换:byte[] getBytes():把字符串转换为byte数组
char[] toCharArray():把字符串转换为char数组
String(byte[] bytes):把byte数组转换为字符串
String(char[] value):把char数组转换为字符串
2):获取字符串信息
int length() 返回此字符串的长度
char charAt(int index) 返回指定索引处的 char 值
int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。
int lastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引
3):字符串比较判断
boolean equals(Object anObject) 将此字符串与指定的对象比较。
boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写
boolean contentEquals(CharSequence cs) 将此字符串与指定的 CharSequence 比较
4):字符串大小写转换
String toUpperCase() 把当前字符串转换为大写(谁调用该方法,谁就是当前字符)
String toLowerCase() 把当前字符串转换为小写
5.写一个String判空工具类
/**
*
* 说明:String判空工具类
*
*/
public class StringUtil {
private StringUtil(){}
/**
* 判断字符串非空,既不是引用为空,也不是空字符
* @param str 传入一个字符串
* @return true:字符串非空;false:字符串为空
*/
public static boolean hasLength(String str) {
return str != null && !"".equals(str.trim());
}
/**
* 判断字符串为空
* @param str 传入一个字符串
* @return true:字符串为空;false:字符串非空
*/
public static boolean isBlank(String str){
return !hasLength(str);
}
}
测试public class TestDemo {
public static void main(String[] args) {
//判断字符串非空
System.out.println(StringUtil.hasLength("abcde"));//true
System.out.println(StringUtil.hasLength(null)); //false
System.out.println(StringUtil.hasLength("")); //false
System.out.println(StringUtil.hasLength(" ")); //false
//判断字符串为空
System.out.println(StringUtil.isBlank("abcde"));//false
System.out.println(StringUtil.isBlank(null)); //true
System.out.println(StringUtil.isBlank("")); //true
System.out.println(StringUtil.isBlank(" ")); //true
}
}