java 的 return 是指標

xfei6868發表於2009-07-26
本題目不是很正確,但是我想說的是java的return 類引用是指標。下面一段程式碼

public class RetTest
{
private TestEntity tsEt;
private String tsStr;
private int tsI;

public RetTest()
{
tsI = 0;
tsStr = "A";
tsEt = new TestEntity();
}

public int GetI()
{
return tsI;
}

public int I1()
{
tsI = 1;
return tsI;
}

public int I2()
{
tsI = 2;
return tsI;
}

public String GetStr()
{
return tsStr;
}

public String Str1()
{
tsStr = "B";
return tsStr;
}

public String Str2()
{
tsStr = "C";
return tsStr;
}

public TestEntity GetEntity()
{
return tsEt;
}

public TestEntity Entity1()
{
//TestEntity tsEt1 = new TestEntity();
tsEt.SetI(1);
tsEt.SetIRef(new Integer(1));
return tsEt;
}

public TestEntity Entity2()
{
tsEt.SetI(2);
tsEt.SetIRef(new Integer(2));
return tsEt;
}

// public static void main(String[] args)
public static void main(String[] args)
{
RetTest retTest = new RetTest();

int Itest = retTest.GetI();
System.out.println("I: " + Itest);

int Itest1 = retTest.I1();
System.out.println("I: " + Itest);
System.out.println("I1: " + Itest1);

int Itest2 = retTest.I2();
System.out.println("I: " + Itest);
System.out.println("I1: " + Itest1);
System.out.println("I2: " + Itest2);

//-----------------------------------------------------

String str = retTest.GetStr();
System.out.println("Str: " + str);

String str1 = retTest.Str1();
System.out.println("Str: " + str);
System.out.println("Str1: " + str1);

String str2 = retTest.Str2();
System.out.println("Str: " + str);
System.out.println("Str1: " + str1);
System.out.println("Str2: " + str2);

//-----------------------------------------------------

TestEntity testEt = retTest.GetEntity();
System.out.println("testEt -> Integer: " + testEt.GetIRef() + ", i:" + testEt.GetI());

TestEntity testEt1 = retTest.Entity1();
System.out.println("testEt -> Integer: " + testEt.GetIRef() + ", i:" + testEt.GetI());
System.out.println("testEt1 -> Integer: " + testEt1.GetIRef() + ", i:" + testEt1.GetI());

TestEntity testEt2 = retTest.Entity2();
System.out.println("testEt -> Integer: " + testEt.GetIRef() + ", i:" + testEt.GetI());
System.out.println("testEt1 -> Integer: " + testEt1.GetIRef() + ", i:" + testEt1.GetI());
System.out.println("testEt2 -> Integer: " + testEt2.GetIRef() + ", i:" + testEt2.GetI());


System.out.println("testEt = testEt1: " + (testEt == testEt1));
System.out.println("testEt = testEt2: " + (testEt == testEt2));
System.out.println("testEt1 = testEt2: " + (testEt1 == testEt2));
}

}

class TestEntity
{
private Integer iRef;
private int i;

public void SetI(int i)
{
this.i = i;
}

public int GetI()
{
return i;
}

public void SetIRef(Integer iRef)
{
this.iRef = iRef;
}

public Integer GetIRef()
{
return iRef;
}
}



返回的結果為:

I: 0
I: 0
I1: 1
I: 0
I1: 1
I2: 2
Str: A
Str: A
Str1: B
Str: A
Str1: B
Str2: C
testEt -> Integer: null, i:0
testEt -> Integer: 1, i:1
testEt1 -> Integer: 1, i:1
testEt -> Integer: 2, i:2
testEt1 -> Integer: 2, i:2
testEt2 -> Integer: 2, i:2
testEt = testEt1: true
testEt = testEt2: true
testEt1 = testEt2: true


可以看出int 是值型別,返回的是值的copy
TestEntity 是一個類, 它返回的情況:

testEt -> Integer: null, i:0
testEt -> Integer: 1, i:1
testEt1 -> Integer: 1, i:1
testEt -> Integer: 2, i:2
testEt1 -> Integer: 2, i:2
testEt2 -> Integer: 2, i:2
testEt = testEt1: true
testEt = testEt2: true
testEt1 = testEt2: true

可以清楚的看出return的就是指標內容

而String型別的怎麼看上去像是基本型別的一樣呢?String也是類啊!
因為在String的物件池裡,"A"、"B"、"C"三個字串都是並沒有存在的,這樣每次String型別的物件每次=的時候相當於都要建立了一個String的物件。當然就如Str = new String("A); Str1 = new String("B"); Str2 = new String("C");這是三個不同的類當然在堆中的地址也是不同的。
由於String物件池的特性,有時候也會產生這樣的情況。如果修改一些程式碼

...
public String Str1()
{
tsStr = "A"; //把“B”改成“A”
return tsStr;
}
...

新增

......
System.out.println("Str = Str1: " + (str == str1));
System.out.println("Str = Str2: " + (str == str2));
System.out.println("Str1 = Str2: " + (str1 == str2));
......

關於String測試的部分就會是:

Str: A
Str: A
Str1: A
Str: A
Str1: A
Str2: C
Str = Str1: true
Str = Str2: false
Str1 = Str2: false

這時候“Str = Str1: true”,要理解這個其實對java中的String很熟悉的話也是很好理解的。因為“A” 在物件池裡已經存在了,這時候=的時候後面是“A”的話,物件的指標就會指向已經存在的“A”,而不在新建物件。
return 針對物件是指標,知道的這點編寫函式的時候,尤其類中全域性變數,在被返回的時候有可能多個函式都返回,這樣就要注意了,return的只不過是指標!

相關文章