• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

toString() operation in '+' operator

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this program

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 ]
 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sumeer

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.
 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was a bug in the JDK's javac. They have fixed it in the latest version (mantis beta).
Check this out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic