1.字符串“+”、StringBuffer、StringBuilder的使用环境
如果使用少量的字符串操作,使用 (+运算符)连接字符串;
如果频繁的对大量字符串进行操作,则使用
全局变量或者需要多线程支持则使用StringBuffer;
如果使用少量的字符串操作,使用 (+运算符)连接字符串;
如果频繁的对大量字符串进行操作,则使用
全局变量或者需要多线程支持则使用StringBuffer;
局部变量或者单线程不涉及线程安全则使有StringBuilder。
2.
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);//true:当值在-128~127之间时,不会new新的对象
System.out.println(f3 == f4);//false:当值不在上面范围之间时,会new新的对象
3.
String s1="test";
String s2=new String("test");
String s3="te"+"st";
System.out.println(s1==s2);//false
System.out.println(s1==s3);//true
System.out.println(s1==s2.intern());//true
4.
class Test{
class Inner{}
public static void foo(){new Test().new Inner();}
public static void foo(){new Inner();}//编译错误。静态方法中没有this,也就是说没有所谓的外部类对象,因此无法创建内部类对象
public void bar(){new Inner();}
}