Hi, Should the following piece of code compile or not: System.out.println(null + true); //1 System.out.println(true + null); //2 System.out.println(null + null); //3 I found it on jdiscuss.com. As per them, the answer should be no 'coz as none of the operands is of type 'String'. But it compiles file. Why does it compile fine? Thanks
Output null nullhello 4sum sum22 Lets take a look at the output for string s2: The reason s2 outputs, 'nullhello' is because two key things: a) operator precedence, b) how the overloaded '+' operator works with strings. 1 the '+' operator get evaluated 1st 2 since '+' is overloaded to perform string concatenation, null is converted into a string object (see output for s1), this result is appended with "hello" 3 the assignment operator assign s2 the newly constructed string "nullhello" also note the output for strings s3 and s4, the + operator is evaluated left to right! so in s3, integer arithmetic is performed 1st and the sum is converted into a string when it's added to the string "sum" the operation is done in the following order ((2+2) + "sum")->(4 + "sum")->("4" + "sum") = "4sum" in s4 since we have a string object on the left side of the + operator the other literals are converted into a string, the operation is done in the following order (("sum" + 2) + 2) -> ("sum2" + 2) = "sum22" I hope this clears things up [ May 20, 2003: Message edited by: Rajinder Yadav ]
When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
Joined: May 15, 2003
Thanks Rajinder. My reason for confusion is that in a '+' operation: 1. If the operands are of primitive type, regular '+' is used. 2. If one of the oprands is of String type then overloaded '+' oepration is calles and the non-string operands are converted to String type and concatenated to String operands i.e. 2+1+"str" will return 3str But if i do (null + null), none of the operands is of type String. Does that mean it since 'null' is a valid assignement for String type, compiler converts 'null' to String automatically using .toString() on String object?
I found it on jdiscuss.com. As per them, the answer should be no 'coz as none of the operands is of type 'String'.
According to the above statement this should also be wrong int i=10; System.out.println(i+i); because i is not a String operand. From my point of view the thing is that the operands should be addition compatible that is the + operator should be allowed in your example the + operator is allowed so its ok. But if you take for example (true + true) this is not allowed so it won't work.
Joined: May 15, 2003
Anupam, i=10; i+i will work fine as 'i' is a primitive type. I guess my whole confusion stems from the fact that in a statement like 'null + true', there is no primitve type and no String type. My uderstanding of 'null' is that its not of String type. Correct me if i am wrong . And 'true' is of type boolean. So why does the '+' operation still work fine by calling '.toString()' on 'null' operand?
String conversion applies only to the operands of the binary + operator when one of the arguments is a String.
However, it seems that also applies if one of the arguments is of type null. For instance: System.out.println( ((String)null) + true);//ok String type System.out.println( ((Object)null) + true);//not ok System.out.println(null + true);//ok, null is of null type