Java Challenge. Explanation
Question #1: class A {} class B { static void m(A a1, A a2) { a1 = a2; } public static void main(String[] args) { A a1 = new A(); A a2 = new A(); m(a1,a2); System.out.print(a1==a2); } } Why the output is false? When we pass the value of an object to a method, we are factually passing the reference to it. The reference of a2 should be assigned to a1, isn't it? If we perform this in a main method, it returns true. Could you explain that please? Question #2: String a = "a"; String b = "b"; String c = a + b; String d = "ab"; == used for object equality. How is it applicable with "ab"? That is not an object at all. Why does c==d return false? How does JVM interpret that c object consisting of two other objects? Could anyone provide a technical explanation for this? Thanks in advance.