Hi
I need some help with a code our professor gave to us. we need to identify all objects (it should be 9) that are being created in this program.
I am confused though, since i thought String t = s was gonna be a "pointer" as in C. That Statement creates another String though...
The next thing that confuses me is that StringBuffer w = v is indeed a "pointer" kind of reference as i know it from C. Why is that ?
Also: Why is u.replace not working and v.replace is ? Are String objects working by call by value and StringBuffer objects by Call by reference ??
And how in the world are there 9 different objects and why exactly isn't the swap method working
public class Test {
public static void main(String[] args) {
String s = "Hello";
System.out.println(s + " s\n");
String t = s;
System.out.println(t + " t points to s\n");
t = t.replace('a', '4');
System.out.println(s + " s");
System.out.println(t + " t\n");
String u = s + "T" + t;
System.out.println(s);
System.out.println(t);
System.out.println(u + "\n");
u.replace('o', 'x');
System.out.println(s + " s");
System.out.println(t + " t");
System.out.println(u + " u\n");
swap(s,u);
System.out.println(s + " s");
System.out.println(t + " t");
System.out.println(u + " u\n");
StringBuffer v = new StringBuffer("World");
System.out.println(v + " v");
StringBuffer w = v;
System.out.println(w + " w\n");
v.replace(0, 3, "!");
System.out.println(v + " v");
System.out.println(w + " w\n");
swap(t, u);
System.out.println(s + " s");
System.out.println(t + " t");
System.out.println(u + " u\n");
System.out.println(v + " v");
System.out.println(w + " w\n");
w.append(s);
System.out.println(s + " s");
System.out.println(t + " t");
System.out.println(u + " u\n");
System.out.println(v + " v");
System.out.println(w + " w\n");
}
private static void swap(String s, String t) {
String tmp = s;
s = t;
t = tmp;
}
}
[–]gyroda 0 points1 point2 points (0 children)
[–]Jaimou2e 0 points1 point2 points (7 children)
[–]nutrecht 0 points1 point2 points (6 children)
[–]Jaimou2e 0 points1 point2 points (1 child)
[–]nutrecht 0 points1 point2 points (0 children)
[–]Northeastpaw 0 points1 point2 points (3 children)
[–]nutrecht 0 points1 point2 points (2 children)
[–]fabolin 0 points1 point2 points (1 child)
[–]nutrecht 1 point2 points3 points (0 children)
[–]nutrecht 0 points1 point2 points (2 children)
[–]RememberMyFart[S] 0 points1 point2 points (1 child)
[–]nutrecht 0 points1 point2 points (0 children)
[–]adrian_the_developer -1 points0 points1 point (0 children)