Java学習ノート01-伝値or伝引用

3676 ワード

突然Javaが値の伝達と引用部分でC言語のポインタ(下位メモリ割り当て)を借りて容易に理解できることを発見した.あるいは、これらの難点はC言語の残された問題がJavaとは関係ないということだ.
Note:伝達値と伝達参照の定義については、次のリンクを参照してください.
Javaメソッドのパラメータがどのように伝達されるかという質問に対して、その答えは、値によって伝達されるのも参照によって伝達されるが、参照物が異なるだけで、結果はJavaによって伝達される値と参照によって伝達されるしかない.
ダイレクトコード
/*
*This Program demonstrate parameter passing in java
*@Version 1.00 
*@Author Scorpion
*/

public class ParamTest{
    public static void main(String[] args){
        /**
        *Test1: Methods can't modified numeric parameters
        */
        System.out.println("TestTrip Value...");
        double percent = 10;
        System.out.println("Before: percent="+percent);
        tripleValue(percent);
        System.out.println("After: percent="+percent);

        /**
        *Test2: Method can change the state of object parameters
        */
        System.out.println("
Testing tripleValue:"); Employee harry = new Employee("Harry",5000); System.out.println("Before: salary = "+harry.getSalary()); tripleSalary(harry); System.out.println("After: salary = "+harry.getSalary()); /** *Test3: Methods can't attach new objects to object parameter */ System.out.println("Testing swap:"); Employee a = new Employee("Alice",70000); Employee b = new Employee("Bob",60000); System.out.println("Before: a=" + a.getName()); System.out.println("Before: b=" + b.getName()); swap(a,b); System.out.println("After: a=" + a.getName()); System.out.println("After: b=" + b.getName()); } public static void tripleValue(double x){ x = 3 * x; System.out.println("End of Method:x="+x); } public static void tripleSalary(Employee x){ x.raiseSalary(200); System.out.println("End of Method x = "+x.getSalary()); } /** *Note: just like pointer */ public static void swap(Employee x,Employee y){ Employee temp = x; x = y; y = temp; System.out.println("End of Method: x="+x.getName()); System.out.println("End of Method: y="+y.getName()); } } class Employee{//simplified Employee class private String name; private double salary; public Employee(String n,double s) { name = n; salary = s; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary(double byPercent) { double raise = salary * byPercent / 100; salary += raise; } }

OUTPUT
TestTrip Value... Before: percent=10.0 End of Method:x=30.0 After: percent=10.0 Testing tripleValue: Before: salary = 5000.0 End of Method x = 15000.0 After: salary = 15000.0 Testing swap: Before: a=Alice Before: b=Bob End of Method: x=Bob End of Method: y=Alice After: a=Alice After: b=Bob
対応C言語フラグメント
Part1: tripleValue
void tripleValue(int a,int b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

void main()
{
  int a = 10;
  int b = 9;
  tripleValue(a,b);
}

Part2:tripleSalary
void tripleSalary(int * salary)
{
   *salary +=2000;
}

void main()
{
  int salary = 5000;
  int* p = &salary;
  tripleSalary(p);
}

Part3:Swap
void swap(int* a,int* b)
{
  int* tmp = null;
  tmp = a;
  a = b;
  b = tmp;
}

void main()
{
  int a = 9;
  int b = 10;
  int *pa = &a;
  int *pb = &b;

  swap(pa,pb);
}