there's no pointer in java, it's just a reference.
I known what your question is, but I don't know the answer clearly.In test1, within the testA method, a will reference to b, but when the method call was end, b no longer exit, so where should a reference? I don't know, someone else?
I known what your question is, but I don't know the answer clearly.In test1, within the testA method, a will reference to b, but when the method call was end, b no longer exit, so where should a reference? I don't know, someone else?
test1好像是说不能改变作为引用所指的东西
public class TestArray
{
TestArray()
{
}
float[] a={1.1f,2.2f,3.3f}; public void abc()
{
float[] b={0,0,0,0};
a=b; } public float[] getA()
{
return a;
}
public static void main(String[] args)
{
TestArray aa=new TestArray();
float[] b;
aa.abc();
b=aa.getA();
System.out.println(b[1]);
}
}
结果是0.0。我现在发现test1难理解。别的还行。对象应该是一个reference,但test1究竟怎么理解?a=b这句话改变了什么?elva(花语) 请注意这个a是由原来的a传递过来的。当然,这里叫什么名字是无所谓的。
高手看看。
//不是用b这个局部变量public class TestArray { public TestArray() {
}public void testA(float[] a)
{
float[] b=new float[a.length];
for(int i=0;i<b.length;i++)
a[i]=i*i; //a=b; System.out.println("2------->"+b) ;
System.out.println("3------->"+"adress:"+a+" value:"+a[1]) ;
}
public static void main(String[] args)
{
float[] a={1.1f,2.2f,3.3f};
TestArray object=new TestArray();
System.out.println("1------->"+"adress:"+a+" value:"+a[1]) ;
object.testA(a);
System.out.println("1------->"+"adress:"+a+" value:"+a[1]) ;
}}//在用b这一局部变量 a=b时public class TestArray { public TestArray() {
}public void testA(float[] a)
{
float[] b=new float[a.length];
for(int i=0;i<b.length;i++)
b[i]=i*i; a=b; System.out.println("2------->"+b) ;
System.out.println("3------->"+"adress:"+a+" value:"+a[1]) ;
}
public static void main(String[] args)
{
float[] a={1.1f,2.2f,3.3f};
TestArray object=new TestArray();
System.out.println("1------->"+"adress:"+a+" value:"+a[1]) ;
object.testA(a);
System.out.println("1------->"+"adress:"+a+" value:"+a[1]) ;
}}
可