gives compile time error complaining about method ambiguity but
outputs nullnull. I know that + operator converts the operands to string if any one of the operand is a string, but i am in the above case. Thanks in advance.
I dont have JDK1.3 but I checked up with 1.4. It does not compile. If there is indeed a discrepancy the importrant question is what is the correct behaviour from certification point of view? Ananda did you type in nul+null or null+null?
Jeroen Wenting's theory does not feel right. Any output from System.out should only happen after null+null (which does not compile). But one never knows. Did it compile for anyone else? [ May 20, 2004: Message edited by: Swamy Nathan ]
If you read the two snippets of code from the j2sdk1.4.2_04 source files, then you will get a better idea what happens. What will be executed is something very like:
The "overloaded" string concatentation operator is defined to work when one operand is a reference to a String and the other is either a primitive, a reference to a String, or a reference to an object. The object reference can be null, in which case the string "null" will be used.
So "" + null, and null + "" are both OK, and result in a String "null". null + null should not work because not one of the operands is a reference to a String. The compiler can now detect this case and gives a compilation error.