本人java菜鸟,刚学习的,有段代码没搞懂,请高手解答一下,请帮忙
package basic.day01;public class Example {
String str = new String("good");
char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
}
package basic.day01;public class Example {
String str = new String("good");
char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
}
本人java菜鸟,刚学习的,有段代码没搞懂,请高手解答一下,请帮忙
package basic.day01;public class Example {
String str = new String("good");
char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
} public void change(String str, char ch[]) {
str = "test ok";
ch[0] = 'g';
}}
and abc呢?
this.str = "test ok";
this.ch[0] = 'g';
}
ex.change(ex.str, ex.ch);
当执行完这个语句后,ex.str为什么还是good?
它存的不是text ok的地址吗?
this.str和str是两个东西.没见过很多constructor都是写的 this.xxx=xxx么.
public void change() {
str = "test ok";
ch[0] = 'g';
}
着时str 就是对象的str.调用时也改为:
ex.change();