java常用类接介绍
1.java.lang.String(学会使用帮助文档);
1.1fianl修饰,不能被其它类继承
1.2String的构造
直接赋值 String s = "hello";字符串常量问题
1.3字符串连接问题+
1.4常用方法
字符串拼接时用这两个类,效率高,节约内存,如果用字符串+
性能差,并且浪费空间,产生很多垃圾
1.java.lang.String(学会使用帮助文档);
1.1fianl修饰,不能被其它类继承
1.2String的构造
直接赋值 String s = "hello";字符串常量问题
1.3字符串连接问题+
1.4常用方法
charAt(),length(),indexOf(),lastIndexOf(),substring(),
字符串拼接时用这两个类,效率高,节约内存,如果用字符串+
性能差,并且浪费空间,产生很多垃圾
StringBuffer线程安全;StringBuilder线程不安全。
StringBuilder 在多线程场景下性能比StringBuffer
要高
package String_LianXi;
public class LianXi_1 {
public static void main(String[] args) {
char[] chars = {'a','b','c','d'};
String s1 = new String(chars);
System.out.println(s1);
System.out.println("1=====================");
String s2 = "hello";
/*称为字符串常量or 字符串字面量 通过构造函数new
* 一定会产生新的空间,通过直接赋值的方式,不一定产生新的空间
* 其实是一种设计模式,称为享元模式
*
*
* */
System.out.println(s2);
System.out.println("2======================");
String s3 = new String(chars ,1,2);
System.out.println(s3);
System.out.println("3=========================");
String s4 = "hello";
String s5 = "world";
String s6 = s4 + s5;
String s7 = "helloworld";
System.out.println(s7 == s6);
String s7_1 = "helloworld";
System.out.println(s7 == s7_1);
String s7_2 ="a"+"b"+"c";//等价于"abc"jvm自动优化
/*认为有一个字符串池的内存空间,里面放字符串常量
* 每次总是先从字符串池中找是否有该常量存在
* 如果有,直接引用,如果没有,新的空间,然后引用
*
*
*
* */
String s8 = "hello" + s5;//new StringBuilder("hello").append(s5);
System.out.println(s8==s7);
System.out.println(s8==s6);
String s9 = "hello"+"world";
System.out.println(s9 == s7);
System.out.println("4========================");
String str1 = "hello"+1+2+3;//hello123
System.out.println(str1);
String str2 = 1+2+3+"hello";//6hello
System.out.println(str2);
}
}