Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain - Binary + Operator

 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bindesh,
How does your statement and your example differ from Valentin's statement? Is it possible that you have made similar statements?
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic