程序运行到:str2=str1时, str1,str2都是reference,指向:this is a test string!对象 使用:str1="this is a test modified string!"; 即:申明str1指向了另一个对象"this is a test modified string!",而str2不变 所以输出的str2为何是“this is a test string!”参考: http://www.csdn.net/develop/Read_Article.asp?Id=22025
class string{ public static void main(String args[]){ StringBuffer str1=new StringBuffer("this is a test string!"); StringBuffer str2; str2=str1; str1=str1.append("this is a test modified string!"); System.out.println(str2); } } 就可,正如hyhu(先飞笨鸟) 所说: String是个类型骗子,String不是基本型,但是却实现了基本型的传递!
java的强项在于它的安全性,特别适合于网络开发。
当str1="this is a test modified string!"; 前面的str1已经销毁!!! String类的学问大了!!!
赞同lyjlee() 的看法,str2=str1句使得这两个变量指向同一地址空间, 但当你再次赋值之后:str1="this is a test modified string!"; str1所指向的是一个新的地址空间,此时 str1和str2是不想等的.
看见四个星星,好羡慕! ---------------------------------------------------------------------------------- class string{ public static void main(String args[]){ String str1="this is a test string!"; String str2; str2=str1; //str2等于"this is a test string!" str1="this is a test modified string!"; /* str2还是等于"this is a test string!",因为当str1被赋予新值后,其值没有赋予str2,所以str2还是保持原值。*/ System.out.println(str2); } } ----------------------------------------------------------------------------------前面几位已经解答清楚了!
str1,str2都是reference,指向:this is a test string!对象
使用:str1="this is a test modified string!";
即:申明str1指向了另一个对象"this is a test modified string!",而str2不变
所以输出的str2为何是“this is a test string!”参考:
http://www.csdn.net/develop/Read_Article.asp?Id=22025
public static void main(String args[]){
StringBuffer str1=new StringBuffer("this is a test string!");
StringBuffer str2;
str2=str1;
str1=str1.append("this is a test modified string!");
System.out.println(str2);
}
}
就可,正如hyhu(先飞笨鸟) 所说:
String是个类型骗子,String不是基本型,但是却实现了基本型的传递!
前面的str1已经销毁!!!
String类的学问大了!!!
但当你再次赋值之后:str1="this is a test modified string!";
str1所指向的是一个新的地址空间,此时 str1和str2是不想等的.
----------------------------------------------------------------------------------
class string{
public static void main(String args[]){
String str1="this is a test string!";
String str2;
str2=str1;
//str2等于"this is a test string!"
str1="this is a test modified string!";
/* str2还是等于"this is a test string!",因为当str1被赋予新值后,其值没有赋予str2,所以str2还是保持原值。*/
System.out.println(str2);
}
}
----------------------------------------------------------------------------------前面几位已经解答清楚了!
如果A=B
如果你修改了B 那么A是不会改变的类似str1=str2
J2SE是基础.
J2EE:用于企业级开发.
J2ME:用于小型移动设备.
过程执行。
不应该把str1的值直接赋给str2.