请看如下题目:
public class testAddress {
private static Integer a=new Integer(5);
private static Integer b=new Integer(6); public static void swap(Integer c,Integer d){
Integer temp;
temp=c;
d=temp;
c=d;
}
public void output(){
System.out.println("a="+a+"\tb="+b);
}
public static void main(String[] args) {
testAddress add=new testAddress();
add.output();
swap(add,a,b);
add.output();
}} 输出结果都是一样,请高手发表下看法;
public class testAddress {
private static Integer a=new Integer(5);
private static Integer b=new Integer(6); public static void swap(Integer c,Integer d){
Integer temp;
temp=c;
d=temp;
c=d;
}
public void output(){
System.out.println("a="+a+"\tb="+b);
}
public static void main(String[] args) {
testAddress add=new testAddress();
add.output();
swap(add,a,b);
add.output();
}} 输出结果都是一样,请高手发表下看法;
解决方案 »
- Java 多线程问题
- 有关String pool 等的几个有关存储区的问题
- java调用ntdll.dll中的函数
- java.lang.ArrayIndexOutOfBoundsException: 1024
- 大家谈谈对Thinking in java 这本书的看法吧
- 【求助】用命令行编译时出现以下情况,还望大虾们指点以下
- 今天正式加盟Java!请教java.lang中Import的类位置.
- 一个关于字符串截取的问题,请帮忙!
- 有没有规定一定要先安装SDK然后才能安装JVM?否则就安装不了SDK?
- java中怎样格式化字符
- 关于hashtable,提示不兼容的类型
- 为什么这么写没有体现出线程的优先级呢?
另外你写的好像有点问题啊
a,b所引用的没啥变化。
public class testAddress {
private Integer a=new Integer(5);
private Integer b=new Integer(6);
private static String val="boss"; public static void swap(Integer c,Integer d,String value){
Integer temp;
temp=c;
d=temp;
c=d;
val=value;
}
public void output(){
System.out.println("a="+a+"\tb="+b+"\tval="+val);
}
public static void main(String[] args) {
testAddress add=new testAddress();
add.output();
swap(add.a,add.b,"employee");
add.output();
}}
不过增加一个String 类型以作参考,
结果是:
a=5 b=6 val=boss
a=5 b=6 val=employee
请问LZ 你这不给val重新赋值了吗当然变了 val=value;
应该是这样..! 自己去试下.!反正我已经调过了.. Integer是不会改value的
为了使对象的内容改变 可以这样写:
public class testAddress {
private Integer a=new Integer(5);
private Integer b=new Integer(6); public static void swap(testAddress c){
Integer temp; temp=c.a;
c.a=c.b;
c.b=temp;
}
public void output(){
System.out.println("a="+a+"\tb="+b);
}
public static void main(String[] args) {
testAddress add=new testAddress();
add.output();
swap(add);
add.output();
}}
1. 这里的交换本身就存在问题。你认真看看?
Integer temp;
temp=c;
d=temp;
c=d;2. 声明了static变量就表示是不能再修改其地址了.
有,但引用传递的参数类型一定要是自己自定义的类型
temp=c;
d=temp;
c=d; 写对了吗?
传对象类型是传引用基本类型的封装类在作为参数传递的时候,为基本数据类型,是传值
1.
楼主的这个写的有问题:
Integer temp;
temp=c;
d=temp;
c=d;
2.
关于传递的问题,除了基本类型传递的时候是值传递之外,其他的类型,也就是各种对象在传递的时候都是采用引用传递。但是必须注意,传递只是传递一个引用的副本。
3.
Integer类型里面的内容是保持不变的,这个跟string有点类似,比如有语句:Integer ite=new Integer(3);ite=5;
这个时候ite开始存放的是Integer(3)的内存地址,后来有存放的是5的内存地址,原来的Integer(3)变成垃圾会被回收。所以,即使上面的交换写的争取,c和d的交换不会影响a和b原来的值,所以结果还是一样的
public static void swap(Integer c,Integer d){
Integer temp;
temp=c;
d=temp;
c=d;
}
这个错了。
还有说明一下:Integer c,Integer d,当你传进参数时产生了两个新的Integer对象,这两个对象拷贝了原来两个对象的值,这样你对他进行交换根本就没有影响到原来的内存区域,当然不会出现你想要的结果了。
你想实现交换的话可能通过自己定义的类型去实现~
public static void swap1(Apple a,Apple b){
Apple c;
c=a;
a=b;
b=c;
}
//值改变
public static void swap(Apple a,Apple b){
Apple c=new Apple();
c.setcolor(a.getcolor());
c.setname(a.getname());
a.setcolor(b.getcolor());
a.setname(b.getname());
b.setcolor(c.getcolor());
b.setname(c.getname());
}
Apple apple1=new Apple();
Apple apple2=new Apple();
swap1(apple1,apple2);
swap(apple1,apple2);
swap1中两个Apple对象进去a指向apple1 b指向apple2 它们相当四个不同指针
通过交换后只是把b指向apple1,a指向apple2;apple1 和apple2依然不会变
而swap中把apple1 和apple2传进去 a与apple1指向同一段内存 b与apple2指向通一段内存
当你修改a的值时 因为apple1和它指向的是同一段内存所以值就会变