如下面代码:
public class StringTest2
{
String s1 = "H";
String s2 = "ello";
String s3 = s1 + s2;
String s4 = "H" + "ello";
static String ss1 = "H";
static String ss2 = "ello";
static String ss3 = ss1 + ss2;
static String ss4 = "H" + "ello";
final String fs1 = "H";
final String fs2 = "ello";
final String fs3 = fs1 + fs2;
final String fs4 = "H" + "ello";
static final String sfs1 = "H";
static final String sfs2 = "ello";
static final String sfs3 = sfs1 + sfs2;
static final String sfs4 = "H" + "ello";
public static void main(String[] args)
{
StringTest2 st = new StringTest2();
st.test1();
}
public void test1()
{
String str1 = "H";
String str2 = "ello";
String str3 = "Hello";
String str4 = str1 + str2;
String str5 = "H" + "ello";
//1
System.out.println(str3 == str4);
System.out.println(str3 == str5);
//2
System.out.println();
System.out.println(str3 == s3);
System.out.println(str3 == s4);
//3
System.out.println();
System.out.println(str3 == ss3);
System.out.println(str3 == ss4);
//4
System.out.println();
System.out.println(str3 == fs3);
System.out.println(str3 == fs4);
//5
System.out.println();
System.out.println(str3 == sfs3);
System.out.println(str3 == sfs4);
}
}
输出如下:
false
true
false
true
false
true
true
true
true
true