---
title: "(转)关于Java的参数传递"
author: "7090"
date: "2011-03-30T13:39:13Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-05-04T11:48:57Z"
id: 980399
url: "https://7090.iteye.com/blog/980399"
---

Java语言的通过方法的参数传递属于值传递，也就是说对传递到方法内部使用的值（无论是基本数据类型，还是引用数据类型）进行修改时，都不会影响到方法外部的传入值。

当参数为基本数据类型时，传递的是基本数据类型的值，当参数为引用数据类型时，传递的是引用数据类型的值，其值可以理解为所指向的对象的地址（指 针），因此方法内部无论对地址作何修改，都不会影响到方法外部引用的值。值得注意的一点是：当在方法内部使用临时的引用变量对对象的值进行修改时，对象的 值是会发生变化的。。

public class   ArgsTest  
{  
public static void main(String\[] args)  
{  
   ArgsTest arg=new ArgsTest();  
   int tempInt=0;

   arg.changeTempInt(tempInt);  
   System.out.println("tempInt"+tempInt);  
   Person person=new Person(10);  
   arg.changePersonValA(person);  
   System.out.println(person.getA());  
   arg.changePersonRef(person);  
   System.out.println(person.getA());  
}

public void changeTempInt(int tempInt){  
   tempInt=1;  
}

public void changePersonValA(Person p){  
   p.setA(20);  
}  
public void changePersonRef(Person p){  
   p=new Person(30);  
}

}  
class Person  
{  
int a=0;  
Person(int changeA){  
   a=changeA;  
}  
public void setA(int changeA){  
   a=changeA;  
}  
public int getA(){  
   return a;  
}

}