一、参数
在java函数中,参数类型分为按引用传递和按值传递。
public class Person{
private String name;
private int age ;
public void change(Person person){
person.name=person.getName()+"-change";
person.age=person.getAge()+1;
}
public static void changeStr(String str){
str="123";
}
}
类似上面的change函数中参数person,传递的为Person对象的地址
Person person = new Person();
可以对该对象内部的非final属性进行修改。
如果是changeStr函数,当调用Person.changeStr(str)时,内部的操作不会影响外部变量str。
当str为person.getName()时,也不会改变。这是函数参数按照值传递的方式来运行。
tip:
对于String,Integer。。(基本数据类型的包装类)他们的值是不可变的。所以传递时按照值传递来进行。
附String源码:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
...
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
...
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。