public class Pass {
public void setValue(int[] ab) {
ab[0] = 0;
ab[1] = 1;
ab = null;
}
public static void main(String[] args) {
Pass pass = new Pass();
int[] ab = new int[2];
ab[0] = 3;
ab[1] = 4;
pass.setValue(ab);
System.out.println(ab[0]);
System.out.println(ab[1]);
}
} //猜猜结果输出的是什么?
public void setValue(int[] ab) {
ab[0] = 0;
ab[1] = 1;
ab = null;
}
public static void main(String[] args) {
Pass pass = new Pass();
int[] ab = new int[2];
ab[0] = 3;
ab[1] = 4;
pass.setValue(ab);
System.out.println(ab[0]);
System.out.println(ab[1]);
}
} //猜猜结果输出的是什么?
解决方案 »
- Java 如何监控文件目录的变化
- TimerTask里面有UpdateTask()吗?
- UndoManager中的canRedo始终返回false.明明能够redo也返回false,为什么?
- 关于JTextPane的光标问题
- 为什么我写的线程使得CPU占用率总是100%?
- public abstract class Enum<E extends Enum<E>> extends Objectimplements Comparable<E>, Serializable <>里边的是什么意思??
- 急需帮助中~~
- 那位高人给各jdk帮助文档的连接,不要sun的,太慢了
- struts2 标签现在在企业应用中还用的多吗?
- 帮忙看个程序。
- 求完美数...没有提示错误. 却也没有结果..是什么原因?.
- java基础问题....菜鸟提问..
都是指向同一个数组因此
ab[0]=0;
ab[1]=1;
就会修改指向的数组元素。而ab=null,也仅仅是将这个引用的拷贝设置为null,它
现在不指向任何东西,可数组还在那里,被main中的引用ab指向着验证了:java的参数传递是传值,
对于基本类型,就是值的拷贝,对于引用类型,就是引用的拷贝。
ab[1] = 1;
ab = null;//这里改变的是ab的指向,ab已经指向了null,但是外部的ab指向不发生变化,原对象的值并没有发生变化
"可数组还在那里,被main中的引用ab指向着"
这句话没有说得太清晰ChDw(米):
"这里改变的是ab所指向对象中的内容,所以外部的ab也会产生变化"
这句话也感觉稍稍有点问题main中的ab其实一直都没有变化,变化的只是它们所指向的内容
public void setValue(String str1) {
System.out.println("str1 = "+str1);
str1 = "2222";
System.out.println("str1 = "+str1);
str1 = null;
} public static void main(String[] args) {
Pass pass = new Pass();
String str = "111";
System.out.println("before setValue str is " + str);
pass.setValue(str);
System.out.println("acton setValue str is " + str);
}
}
----------------------------------------------------------------------
正解,相当于每次赋值都new了一个新的String出来
比如
string str1="111";
str1="222";
执行这两句的意思是,在内存新new一个区域给“222”,然后str1是指向那个新的内存区“222”,而原来的内存区“111”就成为垃圾了