could someone help me and explain the difference in the result between the two codes?
It would be something like: when I use String to instantiate it passes the reference and when I do not use it, it passes the value. am I sure? Code 1: public class Program { public static void main(String[] args) { A a = new A("1"); A c = a; a.a1 = new String("3"); System.out.print(a.a1); System.out.println(c.a1); } } class A{ public String a1; public A(String a1){ this.a1=a1; } } //Result: 33 Code 2: public class Program2{ public static void main(String[] args) { A a = new A("1"); A c = a; a = new A("3"); System.out.print(a.a1); System.out.print(c.a1); } } class A{ public String a1; public A(String a1){ this.a1=a1; } } //Result:31