@tzhechevを明確にするために、オブジェクトはJavaで参照によって渡されるのではなく、オブジェクト参照はJavaで値によって渡されます。これは、Abuが回答で非常に多く述べています。トピックをもう少し明確にするために、この例を見てください。
class Employee
{
String name;
int age;
}
public class ValueOrReference
{
public static void main(String... args)
{
Employee emp1 = new Employee();
emp1.name = "Myself";
emp1.age = 1;
Employee emp2 = new Employee();
emp2.name = "Yourself";
emp2.age = 2;
System.out.println("Before swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
/*
* Now if Objects are passed by Reference
* then if I swap the two objects in a method,
* then the actual objects will reciprocate
* to the said change. Lets find out.
*/
swapObjects(emp1, emp2);
System.out.println("****************************************");
System.out.println("Inside main after swap method.");
System.out.println("After swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
System.out.println("****************************************");
}
private static void swapObjects(Employee emp1, Employee emp2)
{
System.out.println("****************************************");
System.out.println("Inside the swap method.");
Employee temp = emp1;
emp1 = emp2;
emp2 = temp;
System.out.println("After swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
System.out.println("****************************************");
}
}
出力ケース1:
C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************
Inside the swap method.
After swapping values are :
Employee 1 :
Name : Yourself
Age : 2
Employee 2 :
Name : Myself
Age : 1
****************************************
****************************************
Inside main after swap method.
After swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************
ただし、オブジェクトを1つだけ使用し、そのコンテンツを何らかのメソッドで変更すると、驚くべき結果になります。その驚きのために次のコードを試してください:
class Employee
{
String name;
int age;
}
public class ValueOrReference
{
public static void main(String... args)
{
Employee emp1 = new Employee();
emp1.name = "Myself";
emp1.age = 1;
System.out.println("Before swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
/*
* Now if Objects are passed by Reference
* then if I swap the two objects in a method,
* then the actual objects will reciprocate
* to the said change. Lets find out.
*/
modifyObject(emp1);
System.out.println("****************************************");
System.out.println("Inside main after modify method.");
System.out.println("After modifying values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("****************************************");
}
private static void modifyObject(Employee emp1)
{
System.out.println("****************************************");
System.out.println("Inside the modify method.");
emp1.name = "Yourself";
emp1.age = 2;
System.out.println("After modifying values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("****************************************");
}
}
出力ケース2:
C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
****************************************
Inside the modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************
****************************************
Inside main after modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************