import java.util.HashSet;
public class java01 { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ST st=null;
pt(st);
System.out.println("bb:"+(st==null?"空":"非空"));
} public static void pt(ST st){
st=new ST();
System.out.println("aa:"+(st==null?"空":"非空"));
}
}
编程语言引用堆栈形参
public class java01 { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ST st=null;
pt(st);
System.out.println("bb:"+(st==null?"空":"非空"));
} public static void pt(ST st){
st=new ST();
System.out.println("aa:"+(st==null?"空":"非空"));
}
}
编程语言引用堆栈形参
应该是址传递。想当然了。。
因为main方法中的ST没有指向任何堆内存的地址,所以传给pt的地址也是null。
pt中new了一个新的对象,并将地址指向它,实际上是pt栈中的st地址指向新对象,而main栈中的指针还是为null。所以aa非空,bb空。结果还是一样。
只不过传递的值是地址而已。
main方法传给pt的是一个null地址,然后在pt方法栈中,pt的st参数的地址指向了新对象,但是实际上main方法栈中的st参数的地址并没有任何变化。两个st是不同方法栈的参数,所以pt方法中修改了st的堆内存地址,是不会影响到main的st的。如果改成main中的st有一个实际的地址,传给pt后,pt通过这个地址修改堆内存中的实例,main的st所指向的实例也会相应的改变。
第一个错误的,方法中修改地址,调用者也会变:public class java01 { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ST st=new ST();
st.setAge(1);//此处st有指向的地址,并且年龄被赋为1
pt(st);
System.out.println("bb:"+st.getAge());
} public static void pt(ST st){
st=new ST();//此时在方法中改变形参的指向地址
System.out.println("aa:"+st.getAge());
}
}打印的结果是:
aa:0
bb:1
首先我们从这几个基本知识来思考问题
1.形参传递的是引用指向的地址拷贝;
如方法pt中的st,其实是对main函数中的st地址的一份拷贝
2.在方法pt里面,修改st的值,其实是st拷贝的一个指向地址。
你会发现,在pt方法体内改变setAge()会把main里st的age也改变,
这是因为,拷贝的st和main里面的st地址虽然不同,但是指向
成员变量的地址相同,所以在方法pt里面修改的age也会修改main的值。
3.方法体内的局部变量会随着方法的结束消亡。
虽然有st=new St()这样的操作,使st指向了新的地址,但此时只是把
st拷贝地址指向了新地址,而main函数中的st没有重定向,所以虽然pt方法体
内的局布变量消亡了,st指向的地址为空了,但这只是一份拷贝。
你用sys.out把实例输出了看一下一样不一样public class java01 { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) {
ST st = new ST();
st.setAge(1);
pt(st);
System.out.println("main st:" + st + "\n age = " + st.getAge());
} public static void pt(ST st) {
st = new ST();
System.out.println("pt st:" + st + "\n age = " + st.getAge());
}}class ST {
private int age = 0; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
bb:空
形参传递问题,不用纠结这么久吧,书上原例子
如果是基本数据类型,那么里面做什么都与外面无关了
以上为个人理解
再高的房子都是需要房基,如果房基不好,房子建的越高,最后伤得越惨。一个软件工程也一样,如果你连基本的知识都不清楚,都靠调试一点一点不,你的程序永远是个demo
aa:非空
bb:空
pt里面的st存在栈里面,方法结束,就退栈了。
import java.util.HashSet;
public class java01 {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ST[1] st=null;
pt(st);
System.out.println("bb:"+(st==null?"空":"非空"));
}
public static void pt(ST st){
st=new ST();
System.out.println("aa:"+(st==null?"空":"非空"));
}
}
ST[1] st=null;//这是什么写法,奇怪
public class DemoObject{
}
public static void main(String arg[]){
DemoObject object[] = new DemoObject[2];
new ObjectArrayDemo().pt(object);
System.out.println("bb:"+(object[1]==null?"空":"非空"));
}
public void pt(DemoObject[] st){
st[1]=new DemoObject();
System.out.println("aa:"+(st[1]==null?"空":"非空"));
}
}
表达有误,其实我是这个意思。好长时间没作JAVA,见谅......