It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  Please explain - Binary + Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Please explain - Binary + Operator " Watch " Please explain - Binary + Operator " New topic
Author

Please explain - Binary + Operator

Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
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

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
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

Joined: Jul 02, 2002
Posts: 1865
Bindesh,
How does your statement and your example differ from Valentin's statement? Is it possible that you have made similar statements?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
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

Joined: Aug 21, 2001
Posts: 34
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain - Binary + Operator
 
Similar Threads
Garbage collection easy question
Why is 5 getting printed here?
HashMap
Bitwise Unary NOT
Can anybody Explain the Program