[英]Assign instance variables with local variables - Java
When you assign the value of a local string into an instance variable of a class, does it create a new object (String)? 将本地字符串的值分配给类的实例变量时,它是否创建新对象(字符串)?
public void setNumber(String number){
if(number == null || number.length() != 9)
return;
this.number = number;
}
Do this implicity works like this: 做这个隐式的工作是这样的:
this.number = new String(number);
The important detail to understand is when you pass a String
as a parameter to the setNumber
method, you're not passing an object, you're passing a reference so when you do 要理解的重要细节是,当您将String
作为参数传递给setNumber
方法时,没有传递对象,而是传递了引用,所以当您执行
this.number = number;
you're taking the reference passed as a parameter and then assigning it to the this.number
variable. 您将引用作为参数传递,然后将其分配给this.number
变量。
There is no implicit object construction in the aforementioned statement. 上述声明中没有隐式对象构造。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.