1、final,不可以有子类
2、String str = new String(“hello”);
String str2 = “hello”;
3、字符串不可变
final char[] value;
4、String 长度不可变
StringBuilder,StringBuffer是可变的字符串
StringBuilder 是线程安全的
StringBuffer 是线程安全的
char[] value;
5、String类的常用方法
str.length(); //数组使用属性,字符串使用方法
str.trim(); //去掉字符串的前后空格
str.equalsIgnoreCase("ABC"); //不区分大小写
str.charAt(2); //
6、String的构造方法
方法一:对于类类型,直接使用toString()方法来构造
Integer number=3;
String numStr=number.toString();
方法二:对于基本数据类型,可以使用对应的类的静态的toString()方法来构造
int a=3;
byte b=25;
String aStr=Integer.toString(a);
String bStr=Byte.toString(b);
方法三:使用new String(char[] value),用字符数组构造一个String。
char[] chars={'h','e','l'.'l','o'};
String str=new String(chars);
方法四:直接给一个String变量赋值或者使用new String(String value)来构造。当都是直接赋值的方式时,两个字符串用==比较的值为true,当一个是直接赋值,一个用new String,用==比较时为false。
String a="hello";
String b="hello";
String c=new String("hello");
方法五:使用StringBuffer或StringBuilder构造一个String。String是常量类型,如果需要有大量的字符串操作,使用String的执行速度会慢很多。对于有大量字符串操作的,通过使用StringBuffer或StringBuilder完成字符串操作后,再转换成String。
StringBuffer buffer=new StringBuffer("hello");
String buffer1=new String(buffer);
StringBuilder builder=new StringBuilder("hello");
String builder2=new String("hello");