File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() operation in '+' operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() operation in Watch "toString() operation in New topic

toString() operation in '+' operator

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
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 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?
Rajinder Yadav

Joined: May 13, 2003
Posts: 27
Take a look at this program

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="" target="_blank" rel="nofollow">Rajinder Yadav</a>
s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
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?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Sumeer
I found it on 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;
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.
s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
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?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
JLS 5.4 warns:

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

SCJP2. Please Indent your code using UBB Code
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
It was a bug in the JDK's javac. They have fixed it in the latest version (mantis beta).
Check this out.

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
I agree. Here's the link:
subject: toString() operation in '+' operator
It's not a secret anymore!