传值示例: public class PassByQute { /** * 对象类型一般传的是应用 */ public static void main(String[] args) { StringBuffer str=new StringBuffer("hello"); change(str); //传引用,指向原对象 System.out.println(str); } public static void change(StringBuffer str){ str.append(" world!"); } } 引用示例: public class PassByValue { /** * 基本数据类型一般传值 */ public static void main(String[] args) { int x=3,y=4; swap(x, y); //传值是将值拷贝一份到内存,并不会对原值有影响 System.out.println("x="+x+"/ty="+y); } public static void swap(int x,int y){ int temp=x; x=y; y=temp; } } package cn.csu.string; /** * @task 传值和引用 * @author Li an * @version 1.0 * @since Mar 21, 2011 */ public class Swap { /** *基本类型的数据存储于栈内存中,调用内存方法,分别创建了一块新的x,y栈内存 * 栈内存(调用函数时新创建了三个临时变量,原变量的值没有变化) * temp=3 * y=4 3 * x=3 4 * y=4 * x=3 */ static int a,b; public static void swap(int x,int y){ int temp=x; x=y; y=temp; } /** *采用全局变量的方式 */ public static void swapAB(int x,int y){ a=y; b=x; } public static void main(String[] args) { //没有成功交换 int x=3,y=4; swap(3,4); System.out.println("x="+x+" y="+y); //利用全局变量的方式交换 swapAB(x, y); System.out.println("x="+a+" y="+b); } }