只是为了澄清@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
****************************************
虽然如果我只使用一个对象并以某种方法修改它的内容,那么结果会令人惊讶。试试下面的代码以获得惊喜:
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
****************************************