h*****l 发帖数: 2 | 1 May I ask what method is used to copy a String to another? |
s**h 发帖数: 17 | 2 use = operator directly ah...
【在 h*****l 的大作中提到】 : May I ask what method is used to copy a String to another?
|
g*s 发帖数: 2277 | 3
it will point to same object lah.use constructor or substring.
【在 s**h 的大作中提到】 : use = operator directly ah...
|
s**h 发帖数: 17 | 4 oh...I thought it was ok to point to the same object:)
【在 g*s 的大作中提到】 : : it will point to same object lah.use constructor or substring.
|
b****r 发帖数: 53 | 5 String s1="abc";
String s2= new String(s1)
then change of s2 will not affect s1
在 huangzl (gossip) 的大作中提到: 】 |
s**h 发帖数: 17 | 6 String s, t;
s="what are you doing?";
t="what what what";
System.out.println(t);
t=s;
System.out.println(t);
s="1.12.2.2.2.2";
System.out.println(t);
output:
what what what
what are you doing?
what are you doing?
hehe
【在 b****r 的大作中提到】 : String s1="abc"; : String s2= new String(s1) : then change of s2 will not affect s1 : 在 huangzl (gossip) 的大作中提到: 】
|
f****u 发帖数: 12 | 7 class Str {
String str;
}
class HelloWorldApp {
public static void main(String[] args) {
Str s=new Str();
Str t=new Str();
s.str="1";
t.str="2";
System.out.println(t.str);
t=s;
System.out.println(t.str);
s.str="3";
System.out.println(t.str);
}
}
The result is:
2
1
3
If u use "=" to assiagn class2 to class1, both 1 and 2 contain the same
reference--point to the same object. This phenomenon is called "aliasing
【在 s**h 的大作中提到】 : String s, t; : s="what are you doing?"; : t="what what what"; : System.out.println(t); : t=s; : System.out.println(t); : s="1.12.2.2.2.2"; : System.out.println(t); : output: : what what what
|
w*********n 发帖数: 84 | 8 hehe, it's now quite complicated.
I prefer the simple solusion. such, barber's.
【在 f****u 的大作中提到】 : class Str { : String str; : } : class HelloWorldApp { : public static void main(String[] args) { : Str s=new Str(); : Str t=new Str(); : s.str="1"; : t.str="2"; : System.out.println(t.str);
|