1、String
String类(final类)是不可变类,一旦String对象创建之后,包含在这个对象中的字符序列是不可以改变的,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。直到这个对象被销毁。对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象。
(如何理解不可变类:https://blog.csdn.net/Steven_ssm/article/details/77140486)
String 方法:https://www.cnblogs.com/cisum/p/8011485.html
StringBuffer对象代表字符序列可变的字符串,可以使用append、insert、reverse等方法改变这个字符串对象序列,最后可以使用toString方法转化成String对象。
StringBuilder与StringBuffer类似只是线程是安全的。
常量池:在编译期间就被确定、并保存在已编译的.class文件中数据。Java确保常量只有一个,不会产生多个副本。
String s1 = "hello";
用new String()创建的字符串在编译期间不被确定,不会放入常量池中。
String string = new String("agasdgdsagdghktykle");
System.out.println(string.charAt(6));// d
String s1 = new String("abcdef");
String s2 = new String("abcdfg");
System.out.println(s1.compareTo(s2));// 比较 -1
System.out.println(s1.concat(s2));// 拼接
System.out.println(s1.startsWith("a"));
System.out.println(s1.endsWith("e"));
System.out.println(s1.equals(s2));
System.out.println("A".equalsIgnoreCase("a"));
System.out.println(s1.indexOf("c"));
System.out.println(s1.indexOf("d", 2));
System.out.println(s1.lastIndexOf("d"));
System.out.println(s1.length());
System.out.println(s1.substring(1));
System.out.println(s1.substring(1, 3));
System.out.println(s1.toCharArray());// 对象转成数组
System.out.println(s1.toLowerCase());
System.out.println(s1.toUpperCase());
// 常量池
String ss0 = "hello";
String ss1 = "hello";
String ss2 = "hel" + "lo";
System.out.println(ss0 == ss1);// true
System.out.println(ss0 == ss2);// true
System.out.println(new String("a") == new String("a"));// false new的对象不一样
/*
* 常量池,String s1 = "abc"存在常量池中,在次创建的时候
* Java底层优先从常量池查找。
*/
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
2、String、StringBuffer、StringBuilder比较:
String类的内容一旦声明则不可改变,而StringBuffer类与StringBuilder类声明的内容可以改变。
StringBuffer类中提供的方法都是同步方法,属于安全的线程操作,而StringBuilder类中方法属于异步方法,属于非线程安全的操作。
运行速度快慢为:StringBuilder > StringBuffer > String
String最慢的原因: String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的。StringBuilder和StringBuffer的对象是变量,对变量进行操作就是直接对该对象进行更改,而不进行创建和回收的操作,所以速度要比String快很多。
String 字符串常量
StringBuffer 字符串变量(线程安全)
StringBuilder 字符串变量(非线程安全)
3、equals
public class Equals
{
public static void main(String[] args)
{
String a1 = "aaa";
String a2 = "aaa";
String a3 = new String("aaa");
String a4 = new String("aaa");
System.out.println(a1 == a2);//true
System.out.println(a1 == a3);//false
System.out.println(a3 == a4);//false
//不重写的话equals比较的是内容
System.out.println(a1.equals(a2));//true
System.out.println(a1.equals(a3));//true
System.out.println(a3.equals(a4));//true
}
}
https://www.cnblogs.com/xiaoxi/p/6036701.html
https://www.cnblogs.com/lwbqqyumidi/p/4060845.html
4、BigDecimal
可以精确计算和标示浮点数。
不推荐使用构造器方法,有不可预知性。
System.out.println(new BigDecimal(0.1));//0.1000000000000000055511151231257827021181583404541015625
BigDecimal f1=BigDecimal.valueOf(10.5);
BigDecimal f2=BigDecimal.valueOf(1.5);
System.out.println(f1.add(f2));
System.out.println(f1.subtract(f2));//减
System.out.println(f1.multiply(f2));//乘
System.out.println(f1.divide(f2));//除
字符串转成BigDecimal
String string = "china";
BigDecimal bigDecimal = new BigDecimal(string);
BigDecimal bigDecimal = BigDecimal.valueOf(string);//报错