标签:地址 highlight rri nsf sys main col ros turn
1、what?
值传递:
将实际参数的副本 传递,这样对参数作修改,不会影响到实际参数;
引用传递:
将 实际参数的地址 直接 传递,对参数的修改,影响实际参数;
Java中只有 值传递;
public class ValueOrReferenceTransferTest { static String say(String name){ name = "rose"; return name; } static int say(int a){ a = 4; return a; } static Person say(Person person){ person.setName("rose"); return person; } static Person say1(Person person){ return new Person("jack"); } public static void main(String[] args) { String name = "jack"; String s = say(name); System.out.println("name:"+name+" ,s:"+s);// name:jack ,s:rose int a =1; int i = say(a); System.out.println("a:"+a+" ,i:"+i);// a:1 ,i:4 Person person = new Person("rose"); Person person1 = say(person); System.out.println("p:"+person+" ,p1:"+person1);// p:Person{name=‘rose‘} ,p1:Person{name=‘rose‘} Person person2 = say1(person); System.out.println("p:"+person+" ,p2:"+person2);// p:Person{name=‘rose‘} ,p2:Person{name=‘jack‘} } public static class Person{ private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name=‘" + name + ‘\‘‘ + ‘}‘; } } }
标签:地址 highlight rri nsf sys main col ros turn
原文地址:https://www.cnblogs.com/anpeiyong/p/14886326.html