先看下面的代码,有助于后面的理解。
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.show();
}
{
System.out.println(1);
}
static
{
System.out.println(2);
}
public void show()
{
System.out.println(3);
}
{
System.out.println(4);
}
static
{
System.out.println(5);
}
{
System.out.println(6);
}
}
输出为:
2
5
1
4
6
3
现在看看下面的这些String变量,用“==”操作符比较它们时会是什么结果呢?如下代码。
public class StringTest
{
//11
final String fs1 = "H";
final String fs2 = "ello";
final String fs3 = "Hello";
final String fs4 = fs1 + fs2;
final String fs5 = "H" + "ello";
//22
final String fs11;
final String fs22;
final String fs33;
final String fs44;
final String fs55;
//static //不能有static
{
fs11 = "H";
fs22 = "ello";
fs33 = "Hello";
fs44 = fs11 + fs22;
fs55 = "H" + "ello";
}
//**********************
//**********************
//33
static String ss1 = "H";
static String ss2 = "ello";
static String ss3 = "Hello";
static String ss4 = ss1 + ss2;
static String ss5 = "H" + "ello";
//44
static String ss11;
static String ss22;
static String ss33;
static String ss44;
static String ss55;
static //注释掉也一样
{
ss11 = "H";
ss22 = "ello";
ss33 = "Hello";
ss44 = ss11 + ss22;
ss55 = "H" + "ello";
}
//**********************
//**********************
//55
static final String sfs1 = "H";
static final String sfs2 = "ello";
static final String sfs3 = "Hello";
static final String sfs4 = sfs1 + sfs2;
static final String sfs5 = "H" + "ello";
//66
static final String sfs11;
static final String sfs22;
static final String sfs33;
static final String sfs44;
static final String sfs55;
static //不能注释掉
{
sfs11 = "H";
sfs22 = "ello";
sfs33 = "Hello";
sfs44 = sfs11 + sfs22;
sfs55 = "H" + "ello";
}
public static void main(String[] args)
{
StringTest test = new StringTest();
test.test1();
System.out.println();
test.test2();
System.out.println();
test.test3();
System.out.println();
test.test4();
}
public void test1()
{
System.out.println(fs3 == fs4);
System.out.println(fs3 == fs5);
System.out.println(fs3 == fs33);
System.out.println(fs3 == fs44);
System.out.println(fs3 == fs55);
System.out.println(fs33 == fs44);
System.out.println(fs33 == fs55);
}
public void test2()
{
System.out.println(ss3 == ss4);
System.out.println(ss3 == ss5);
System.out.println(ss3 == ss33);
System.out.println(ss3 == ss44);
System.out.println(ss3 == ss55);
System.out.println(ss33 == ss44);
System.out.println(ss33 == ss55);
}
public void test3()
{
System.out.println(sfs3 == sfs4);
System.out.println(sfs3 == sfs5);
System.out.println(sfs3 == sfs33);
System.out.println(sfs3 == sfs44);
System.out.println(sfs3 == sfs55);
System.out.println(sfs33 == sfs44);
System.out.println(sfs33 == sfs55);
}
public void test4()
{
System.out.println(fs3 == ss3);
System.out.println(fs3 == ss4);
System.out.println(fs3 == ss33);
System.out.println(fs3 == ss44);
System.out.println(fs3 == sfs4);
System.out.println(fs3 == sfs44);
}
}
输出为:
true
true
true
false
true
false
true
false
true
true
false
true
false
true
true
true
true
false
true
false
true
true
false
true
false
true
false