JAvaにおけるパラメータ伝達問題の解釈(伝達値と伝達参照のいくつかの問題の解釈)
まず、Javaパラメータ伝達に関する個人的な見方を説明します.
実際には、Javaの8つの基本データ型が直接値をコピーすることによって伝達されるか、カスタムタイプを含む他のタイプがいわゆる参照によって伝達されるかにかかわらず、1つの値を渡すことによって、1つのタイプのコピー(基本タイプは直接値をコピーし、参照方式はアドレスの値をコピーする)である.
実際には、Javaの8つの基本データ型が直接値をコピーすることによって伝達されるか、カスタムタイプを含む他のタイプがいわゆる参照によって伝達されるかにかかわらず、1つの値を渡すことによって、1つのタイプのコピー(基本タイプは直接値をコピーし、参照方式はアドレスの値をコピーする)である.
package cn.edu.nwsuaf.cie.qhs;
/**
*
* @author ( )
*
*/
public class ArgumentPassing {
/**
* , Java 、
* swap(*,*)
* 1. swap()
* 2. swap(int,int)
* 3. swap(class,class)
* class , , String
* 3.1 swap(String,String)
* 3.2 swap(ArgumentPassing,ArgumentPassing)
* 4. swap(int[],int pos1,int pos2)
* 5. Cloneable ,
*/
private int num1 = 1;
private int num2 = 2;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
/**
* swap()
*/
public void swap(){
int temp = this.getNum1();
this.setNum1(this.getNum2());
this.setNum2(temp);
}
/**
* int swap(int,int)
*/
public void swap(int num1, int num2){
int temp = num1;
num1 = num2;
num2 = temp;
}
/**
* String swap(String,String)
*/
public void swap(String str1,String str2){
String tempStr = str1;
str1 = str2;
str2 = tempStr;
}
/**
* ArgumentPassing swap(ArgumentPassing,ArgumentPassing)
*/
public void swap(ArgumentPassing arg1,ArgumentPassing arg2){
int num1 = arg1.getNum1();
arg1.setNum1(arg2.getNum2());
arg2.setNum2(num1);
}
/**
* int[] swap(int[],int pos1,int pos2)
*/
public void swap(int[] array,int pos1,int pos2){
int temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
}
public String toString(){
return "This object contains paramaters such as below: num1 = "+ this.getNum1()+";num2="+this.getNum2();
}
public void test(){
ArgumentPassing argPassing = new ArgumentPassing();
int num1 = 1;
int num2 = 2;
System.out.println(" swap() :num1="+argPassing.getNum1()+";num2="+argPassing.getNum2());
argPassing.swap();
System.out.println(" swap() :num1="+argPassing.getNum1()+";num2="+argPassing.getNum2());
/**
* :
* swap() :num1=1;num2=2
* swap() :num1=2;num2=1
*/
System.out.println("******************************************************************************************");
System.out.println(" swap(int,int) :num1="+num1+";num2="+num2);
argPassing.swap(num1, num2);
System.out.println(" swap(int,int) :num1="+num1+";num2="+num2);
/**
* :
* swap(int,int) :num1=1;num2=2
* swap(int,int) :num1=1;num2=2
*/
/**
* ?
* , num1,num2 , , 。
* , , ( ) 。
*/
System.out.println("******************************************************************************************");
String str1 = "This is str1";
String str2 = "This is Str2";
System.out.println(" swap(String,String) :str1="+str1+";str2="+str2);
argPassing.swap(str1, str2);
System.out.println(" swap(String,String) :str1="+str1+";str2="+str2);
/**
* :
* swap(String,String) :str1=This is str1;str2=This is Str2
* swap(String,String) :str1=This is str1;str2=This is Str2
*/
/**
* ? ? ? C++ C++ , ,
* C++ , ! ? , 。
*/
System.out.println("******************************************************************************************");
ArgumentPassing arg1 = new ArgumentPassing();
ArgumentPassing arg2 = new ArgumentPassing();
System.out.println(" swap(ArgumentPassing,ArgumentPassing) :arg1="+arg1.toString()+";arg2="+arg2.toString());
argPassing.swap(arg1, arg2);
System.out.println(" swap(ArgumentPassing,ArgumentPassing) :arg1="+arg1.toString()+";arg2="+arg2.toString());
/**
* :
* swap(ArgumentPassing,ArgumentPassing) :arg1=This object contains paramaters such as below: num1 = 1;num2=2;arg2=This object contains paramaters such as below: num1 = 1;num2=2
* swap(ArgumentPassing,ArgumentPassing) :arg1=This object contains paramaters such as below: num1 = 2;num2=2;arg2=This object contains paramaters such as below: num1 = 1;num2=1
*/
/**
* ? ? 。
* Java , ( ( )),
* swap(String,String) , ( ), 。
* , , 。
* Java , , 。
*/
System.out.println("******************************************************************************************");
int [] array = {1,2,3,4};
int pos1 = 0;
int pos2 = 1;
System.out.println(" swap(int[],int pos1,int pos2) :array[pos1]="+array[pos1]+";array[pos2]="+array[pos2]);
argPassing.swap(array, pos1, pos2);
System.out.println(" swap(int[],int pos1,int pos2) :array[pos1]="+array[pos1]+";array[pos2]="+array[pos2]);
/**
* :
* swap(int[],int pos1,int pos2) :array[pos1]=1;array[pos2]=2
* swap(int[],int pos1,int pos2) :array[pos1]=2;array[pos2]=1
*/
/**
* , swap(ArgumentPassing,ArgumentPassing) , 。 , ,
* , , , , 。
*/
/**
* , Cloneable ? , , ,
* , , , , =, , Java , Cloneable 。
* clone, 。
* , , , , , , 。
*/
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArgumentPassing argPassing = new ArgumentPassing();
argPassing.test();
}
}