java 的 return 是指针

本题目不是很正确,但是我想说的是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的只不过是指针!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值