参数名称

参数传递方式  1. 值传递 2. 引用传递.

1. 值传递(Java的方式):传递的是的值的拷贝

2. 引用传递:传递的是的存储值的引用

 

由于值传递的方式注定了Java的方法的功能:

1. 在方法体内都,无法修改作为参数的基本类型值

2. 在方法体内部,能够修改对象的内容。

3. 在方法体内部,无法将参数外的引用指向新的对象

View Code
public class Employee{

      public double salary=5000.0d;    
}

public class EmployeeTest{
    
     public static void changePrimitiveType(int x){
           x = 2;
     }    
    
     public static void changeObjectContent(Employee employee){
          employee.salary *= 1.1d;          
     }
    
    public static void changeObjectReference(Employee employee){
         employee = new Employee();
    }

    public static void main(String[] args){
          int x = 3;
          System.out.println("Before change method x := " + x);
          changePrimitiveType(x);
          System.out.println("After change method x : =  " + x);
          
          Employee employee = new Employee();
          System.out.println("Before change method" + employee.salary);
          changeObjectContent(employee);
          System.out.println("After change method" + employee.salary);
          
           Employee mEmployee = new Employee();
           System.out.println("Before change method " + mEmployee.salary );
           mEmployee.salary = 2000.0d;
           changeObjectReference(mEmployee);
           System.out.println("After change method " + mEmployee.salary);
    }
}
    
posted @ 2012-09-25 22:05  papertiger  阅读(246)  评论(0)    收藏  举报