Forums Register Login

Please explain - Binary + Operator

+Pie Number of slices to send: Send
System.out.println("5" + null);
System.out.println(5 + null);
I get 5null for both the statements.
I cannot even compile when I use *, /, % or - operators in the second statement.
I understand that when one of the operands is a String in a binary + expression, the other operand is made a String object and a String concatenation happens.
But with null...!!! Please explain
[ July 22, 2002: Message edited by: Madan, Gopal ]
+Pie Number of slices to send: Send
From JLS 15.18.1 String Concatenation Operator +


Now only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

 
+Pie Number of slices to send: Send
No thats not right
The + operator is used to concatenate 2 values in System.out.println(),no matter what the type is.
as an example:

Thanks..
Bindesh Vijayan
[ July 22, 2002: Message edited by: Bindesh Vijayan ]
+Pie Number of slices to send: Send
Bindesh,
How does your statement and your example differ from Valentin's statement? Is it possible that you have made similar statements?
+Pie Number of slices to send: Send
Yes I have made similar statement as that of Valentins with the exception of JLS being transformed to an easy example to make it more easy to understand.
Thanks
Bindesh Vijayan
+Pie Number of slices to send: Send
Yes I have made similar statement as that of Valentin's with the exception of JLS being transformed to an easy example to make it more easy to understand.
Thanks
Bindesh Vijayan
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 560 times.
Similar Threads
Can anybody Explain the Program
Garbage collection easy question
HashMap
Why is 5 getting printed here?
Bitwise Unary NOT
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:02:05.