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.
SCJP 1.4
Swamy Nathan
Ranch Hand
Joined: May 16, 2004
Posts: 187
posted
0
Both ur statements wont compile but this will
System.out.println(""+null+null);
I suspect it has something to do with StringBuffers and that appending a null gives a appending of a "null" text literal
ananda s. roy
Greenhorn
Joined: May 13, 2004
Posts: 12
posted
0
i have compiled the with jdk 1.3._01 version without error giving nullnull as output. :roll:
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
posted
0
null objects are treated as a String "null" by output mechanisms producing Strings. null + null will therefore generate "null" + "null" = "nullnull".
42
Sanyev Babu
Ranch Hand
Joined: Dec 18, 2003
Posts: 132
posted
0
well for me null+null is giving compile time error.
The operator + is undefined for the argument type(s) null, null
Swamy Nathan
Ranch Hand
Joined: May 16, 2004
Posts: 187
posted
0
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 ]
ananda s. roy
Greenhorn
Joined: May 13, 2004
Posts: 12
posted
0
compiles & run without error or warning both on jdk 1.3.x and 1.4.x. . still
Ashok Mash
Ranch Hand
Joined: Oct 13, 2000
Posts: 1936
posted
0
Yup, System.out.println(null+null) prints "nullnull" in jdk 1.3.1_06 for me. I don't know why though! Must be one of those things they fixed later!
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:
hello, whit a string that work but whit a primitive doesnt,if we try this, we get a complier error: System.out.println(null+ null+1);
Congratulation Barry in you post number 2500!
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
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.