能否对基本类型进行引用传递 有人问过这个问题了:http://expert.csdn.net/Expert/topic/1960/1960428.xml?temp=.1887628再就是建议你用java的时候不要总想着C++ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以放到数组里。直接传递,是不可能的。void swap(int[] a){int tmp=a[0];a[0]=a[1];a[1]=tmp;} 好像没有别的方法~你就把他们设为全局变量吧。或者用对象把它封装起来,当一个对象实例作为一个参数传给方法时,实际参数的值是该对象的引用。public class PassTest { float ptValue; public void changeObjValue(PassTest ref) { ref .ptValue = 99.0f; } public static void main(String args[]) { String str; int val; PassTest pt = new PassTest(); val = 11; pt.ptValue = 101.0f; pt.changeObjValue(pt); System.out.println("Current ptValue is:"+pt.ptValue); }}Current ptValue is:99.0pt是一个当前类的实例(即对象),pt实际上是指向该对象的引用。pt作为实在参数传递给changeObjValue()方法,虽然pt本身不改变,但对象的内容可以改变,所以在对象的成员变量ptValue改变后,打印的值为99.0。 如何进行整合? 又出事了~晕咯~快帮我看看怎么回事这代码 用例图画不来啊! JDK新手求教 怎样用DataInputStream从文件读入String? jsp中的javascript问题 变量的作用域 怪,怪,怪,一个超级奇怪的问题! 关于“comment documentation”问题 Rose 这东西有什么优缺点? 请大家发言! 发言必有分!!!! 谁能说说JAVA有什么好书,最好能有电子版的下载地址~! 请问为什么不能在构选函数中初使化字符串?
直接传递,是不可能的。
void swap(int[] a)
{
int tmp=a[0];
a[0]=a[1];
a[1]=tmp;
}
你就把他们设为全局变量吧。或者用对象把它封装起来,当一个对象实例作为一个参数传给方法时,实际参数的值是该对象的引用。public class PassTest {
float ptValue;
public void changeObjValue(PassTest ref) {
ref .ptValue = 99.0f;
}
public static void main(String args[]) {
String str;
int val;
PassTest pt = new PassTest();
val = 11;
pt.ptValue = 101.0f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is:"+pt.ptValue);
}
}Current ptValue is:99.0
pt是一个当前类的实例(即对象),pt实际上是指向该对象的引用。pt作为实在参数传递给changeObjValue()方法,虽然pt本身不改变,但对象的内容可以改变,所以在对象的成员变量ptValue改变后,打印的值为99.0。