Code:
package reference;
class Reference {
static Integer inc(Integer x) {
++x;
return x;
}
static String hello(String s) {
s = "Zdravo, svete!";
return s;
}
public static void main(String[] args) {
Integer x = new Integer(55);
System.out.println(inc(x));
System.out.println(x);
String s = "Hello, world!";
System.out.println(hello(s));
System.out.println(s);
}
}
/** Output
56
55
Zdravo, svete!
Hello, world!
*/
package reference;
class Reference {
static Integer inc(Integer x) {
++x;
return x;
}
static String hello(String s) {
s = "Zdravo, svete!";
return s;
}
public static void main(String[] args) {
Integer x = new Integer(55);
System.out.println(inc(x));
System.out.println(x);
String s = "Hello, world!";
System.out.println(hello(s));
System.out.println(s);
}
}
/** Output
56
55
Zdravo, svete!
Hello, world!
*/
Dakle, bez obzira na pricu o referencama, ovo je fakticki prenosenje po vrednosti. Kako da napisem funkciju koja menja objekat koji je prosledjen kao argument?
Nije bitno koji su zaključci izvučeni, već kako se do njih došlo.