值传递
package com.oop; //值传递 public class Demo04 { public static void main(String[] args) { int a =1; System.out.println(a);//1 Demo04.change(a); System.out.println(a);//1 } //返回值为空 public static void change(int a){ a = 10; } }
package com.oop.demo01; //引用传递:对象本质还是值传递 public class Demo05 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name);//null change(person); System.out.println(person.name);//taimi } public static void change(Person person){ person.name ="taimi"; } } //定义了一个Person类 有一个属性 : name class Person{ String name;//null }
当形式参数是对象person时,则改变了实际对象name的值!!!