Java Challenge. Explanation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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.

30th Aug 2018, 11:58 AM
Steppenwolf
Steppenwolf - avatar
6 Answers
+ 1
#1 so variables for objects in Java are secretly pointers. If you reassign the pointer in the method, the local pointer points to a new place. If you access properties from that pointer, you are modifying the object reference that is being pointed at. So if you tried to assign a new array, this won't be reflected outside the method. If you modify the array through its pointer, these will be preserved. It's all about what the variable represents. #2 "ab" is a string literal and points to an object in the string pool. d points to the same string in the string pool. If I now do new String("ab"); I create a new string object with the same value, but the key thing this is a new object in the String pool. This is why == sometimes returns true, but shouldn't be relied on to decide string equality. So internally, String concatenation uses a stringbuilder which will return a new String object. so "a" + "b" is not the same as "ab" (necessarily :p there could be some compiler optimizations that make this true)
30th Aug 2018, 2:10 PM
Dan Walker
Dan Walker - avatar
+ 2
It seems to me for the #1 that the answer is false because class A and B are not connected. Two different objects are made of class A , but they are not equal. Then trying to call a method m which class A don't have.
30th Aug 2018, 1:27 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Yeah, good to know about the internals, though I think I've only ever used it to clarify questions like this :p It probably arises because you use string literals more often on places like Sololearn when you're testing code, but in production it hardly ever comes up because all the strings are initialised from different files/databases etc. so you're always creating new strings
30th Aug 2018, 5:16 PM
Dan Walker
Dan Walker - avatar
0
In the method m those values are local to the scope of the method. You are assigning a2 to the LOCAL variable a1, not the actual object variable you passed in. "ab" is a String, which is an object in Java Also in Java, you cannot compare strings with == (consistently at least) because what looks like the same string may actually be pointing to two separate String objects, so the object references will be different. (In C++ and other languages this is not the case, Java is slightly unusual in this manner, so you should use .equals() to check for string equality) Edit: also, == is used for primitives in addition to object equality
30th Aug 2018, 12:33 PM
Dan Walker
Dan Walker - avatar
0
#1 Consider this. Arrays are treated as objects in Java. Modified array is still accessible in the main method. Why these changes are not local and do not remain within a method scope? public class Program { static void func (int[] arr) { arr[1] = 5; } public static void main(String[] args) { int arr[] = {1,2,3}; func(arr); System.out.println(arr[1]); } } I cannot get the difference between this example and that one with assigning a variable. #2 Actually, I am aware of all those differences between == and .equals() and I have an experience in working with Java. But there are some details that still bother me. Perhaps, I haven't clarified my question enough. Consider this: 1. ("ab" == d) returns true Here d is pointing to a certain object in a memory. Since the expression returns true, it has to mean that object "ab" (that even hasn't been assigned to any variable) points to the same location in memory. How do we achieve that? Why "ab" is pointing to the same location where d points? 2. (c == d) returns false. Where does c point? It consists of two other variables. Did JVM create a new object in a memory when assigned the sum of a and b to c?
30th Aug 2018, 1:14 PM
Steppenwolf
Steppenwolf - avatar
0
Thank you. Now it seems to be much more clear to me. I definitely need to read some literature about that Java String Pool. ;)
30th Aug 2018, 2:20 PM
Steppenwolf
Steppenwolf - avatar