叨逼叨两句
其实程序员工作上最大的敌人不是身体劳累,而是被生活琐事分心。
String类
构造方法
- public String():空构造
- public String(byte[] bytes):把字节数组转成字符串
- public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
- public String(char[] value):把字符数组转成字符串
- public String(char[] value,int index,int count):把字符数组的一部分转成字符串
- public String(String original):把字符串常量值转成字符串。
常见面试题
视频12.05的题5值得再看一遍。
package test_heima;
public class Demo2 {
public static void main(String[] args) {
//题1:结果是true和true,原因是“abc”进入了常量池
/*String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));*/
//题2:这句话创建了2个对象,一个在堆,一个在常量池,在堆上的那个的内容等于常量池那个,相当于一个复制体
//String s1 = new String("abc");
//题3:结果为false和true
/*String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));*/
//题4:结果都是true,因为java有常量优化机制
/*String s1 = "a"+"b"+"c";
String s2 = "abc";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));*/
//题5:第一个结果false,第二个结果true,变量s1和常量c相加,其实内部是先新建了一个StringBuilder(Buffer)
//对象然后将常量c用append方法给添加了进来,接着再使用toString方法将StringBuilder(Buffer)转换为String类型,
//此时s3指向的也就是这个toString转换过来的对象
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2);
System.out.println(s3.equals(s2));
}
}
String类的判断功能
- boolean equals(Object obj)
- boolean equalsIgnoreCase(String str)
- boolean contains(String str)
- boolean startsWith(String str)
- boolean endsWith(String str)
- boolean isEmpty()
区分null和“”
- “”是String类的实例,可以调用String类的方法
- null是空常量,不能调用任何的方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。