public class PassTest{
float ptValue;

public static void main (String [] args){
String str;
int val;
PassTest pt = new PassTest();
val = 11;
pt.changInt(val);
System.out.println("Int value is : " + val );

str = new String ("hello");
System.out.println("Str value is : " + str); pt.ptValue = 101f;
pt.changeObjValue(pt);
System.out.println("Current ptValue is : " +pt.ptValue);
} public void changInt ( int value ){
value = 55;
}

public void changeStr ( String value ){
value = new String ("dfferent");

} public void changeObjValue(PassTest ref){
ref.ptValue = 99f;
}
}
这里的val和str的值怎么没有改变呀。。如果我要改天它们的值应该怎么修改呢?
这是书上的例题。看不懂了,请教下大家。。我试着改天val和str的值,可是编译通不过

解决方案 »

  1.   

    public class PassTest {
    float ptValue; public static void main(String[] args) {
    String str;
    int val; PassTest pt = new PassTest();
    val = 11;
    pt.changInt(val);//调用方法changInt(val) 将11赋给临时变量 value 再对value进行操作
     //只是改变了value的值,val自然不会改变
    System.out.println("Int   value   is   :   " + val); str = new String("hello");//同上
    System.out.println("Str   value   is   :   " + str); pt.ptValue = 101f;
    pt.changeObjValue(pt);//调用方法后,pt和ref都指向对象 new PassTest()
      //就好比你(pt)和我(ref)都有一个房间(new PassTest())的钥匙,我们都可以进房;
      //我进房间把房里的花瓶(ptValue)里的兰花(101f)换成茶花(99f);
      //你再进房时,房里的花瓶里就一定会是茶花(99f)而非兰花(101f)了
    System.out.println("Current   ptValue   is   :   " + pt.ptValue);
    } public void changInt(int value) {
    value = 55;
    } public void changeStr(String value) {
    value = new String("dfferent"); } public void changeObjValue(PassTest ref) {
    ref.ptValue = 99f;
    }
    }
    说明了一下,不知道你看得懂否
    你最好找本书看一下,自己仔细体会下!
      

  2.   

    你要改变它们的值的话,可以直接赋啊
    如果要调方法的话:
    1.可以将方法的返回值赋给变量;
    2.作为类的属性修改,就像变量pt.ptValue 一样.
      

  3.   

    非常感谢楼上的。。基本上明白勒。那如果我要通过效用方法改变val和str的值,应该怎么修改呢?
    我改了后一直通不过编译!
      

  4.   

    把val和str放到main外,和ptValue放在一起
    然后
    public void changeVal(int val){
      this.val=val;
    }public void changeStr(String str){
      this.str=str;
    }
      

  5.   

    public class PassTest {
    float ptValue; public static void main(String[] args) {
    String str;
    int val;
    PassTest pt = new PassTest();

    val = 11;
    val = pt.changInt(55);
    System.out.println("Int   value   is   :   " + val); str = new String("hello");
    str = pt.changeStr("dfferent");
    System.out.println("Str   value   is   :   " + str); pt.ptValue = 101f;
    pt.changeObjValue(pt);
    System.out.println("Current   ptValue   is   :   " + pt.ptValue);
    } public int changInt(int value) {
    return value;
    } public String changeStr(String value) {
    return value; } public void changeObjValue(PassTest ref) {
    ref.ptValue = 99f;
    }
    }
    我把你代码改了下~!是调用方法的,你试试~!!!
      

  6.   


    public class PassTest {
    float ptValue;
    static int val=11;
    static String str = new String("hello");
    public static void main(String[] args) { PassTest pt = new PassTest();

    pt.changInt(val);
    System.out.println("Int   value   is   :   " + val); 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 changInt(int value) {
    value = 55;
    this.val=value;
    } public void changeStr(String value) {
    value = new String("dfferent");
            this.str = value; 
    } public void changeObjValue(PassTest ref) {
    ref.ptValue = 99f;
    }
    }不好意思~~发错了~!应该是这个!!嘿嘿~!
      

  7.   

    7楼的意思我基本明白了,但是你的代码,我编译通不过。。这是为什么呀?错误提示如下:
    符号: 类 tring
    位置: 类 PassTest
        tring str;
        ^
    PassTest.java:10: 无法从静态上下文中引用非静态 变量 val
            val = 11;
            ^
    PassTest.java:11: 无法从静态上下文中引用非静态 变量 val
            pt.changInt(val);
                        ^
    PassTest.java:13: 无法从静态上下文中引用非静态 变量 val
            System.out.println("Int   value   is   :   " + val);
                                                           ^
    PassTest.java:15: 无法从静态上下文中引用非静态 变量 str
            str = new String("hello");
            ^
    PassTest.java:16: 无法从静态上下文中引用非静态 变量 str
            System.out.println("Str   value   is   :   " + str);我看不懂(无法从静态上下文中引用非静态变量 )这句话。。
    高手指点下!
      

  8.   

    啊???通不过吗??我这里运行得很好啊???
    Int   value   is   :   55
    Str   value   is   :   dfferent
    Current   ptValue   is   :   99.0
    这个是结果啊~~!
    你把我得代码全靠下来~!试试!!
    因为我把你定义变量得位置变了,而且设置为静态得了!!呵呵~!
      

  9.   

    关于参数传递,JAVA语言的语法规定:
    1、基本数据类型和字符串按值传递
    2、数组和除字符串以外的所有对象按址传递
      

  10.   

    要弄清这个问题必须要搞清程序执行时的内存分配。
    内存在程序运行是分四个块,栈、堆、还有两个部分(其中一个载入class类,还有一块存储类的成员变量和字符串)。
    当我们声明一个变量是在栈中分配内存,如果是基本数据类型就直接赋值。如果是引用类型就会在堆空间中开辟内存,而在栈中存放的是一个地址,这个地址指向堆中一块内存,当我们调用一个方法的时候会将这个地址传给形参,这样就都指向了堆中同一块内存,对形参的改变理所当然的会改变原来的值(因为都指向同一块堆内存),但是string 类型是不可变的,所以这么改变不了,如果用stringbuffer这可以改变,但是又不能用new stringbuffer来改变,因为这么做其实是开辟了一个新的堆空间,你可以直接的用stringbuffer类的方法来改变这个串,有效的。
    最好自己来测试一下。