日期:2014-05-20  浏览次数:20663 次

为什么 会这样=== java参数传递
public class PassTest {
float ptValue; 
  public static void main (String args[]) { 
String str; 
int val; 
PassTest pt = new PassTest (); 
val = 11; 
pt.changeInt(val); 
System.out.println ("Int value is: " + val); 
str = new String ("hello"); 
pt.changeStr(str); 
System.out.println ("Str value is: " + str); 
pt.ptValue = 101f; 
pt.changeObjValue(pt); 
System.out.println ("Current ptValue is: " + pt.ptValue); 
  } 
  public void changeInt (int value) { 
value = 55; } 
  public void changeStr (String value) { 
value = new String ("different"); } 
  public void changeObjValue (PassTest ref) { 
ref.ptValue = 99f; } 



结果:Int value is: 11
Str value is: hello
Current ptValue is: 99.0


------解决方案--------------------
changeObjValue调用时传的对象啊,所以里边ptValue变了,pt,ref的ptValue都跟着变了。结果正确。
------解决方案--------------------

这道题不难。
要静下心来想就可以了
要一步一步来
如果你只是应付像SCJP那样的考试基本你去被类型就可以了
背这道类型吧

我是大二的学生
前几天这样的题我也很困惑因为基本书上说JAVA中只有传参数传的是值传递所以不能
用C又值和址传递那样的想法去传

后来我坐在图书馆蹲了4个小时外加睡觉前的2小时终于搞懂这是为什么了
如果你要搞个明白
建议你好好研究下传递参数
对参数的更改到底改的是什么。。。