wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multiple '+' opeartors in a statement 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 "Multiple Watch "Multiple New topic
Author

Multiple '+' opeartors in a statement

Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
I have quite a good experience in Java but i was not at all aware that the following code will not give any compiler error...




Had this been asked in the exam i would have opted for compiler error. Can someone throw some light why this is a valid statement in Java...


-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Lots of "+" which represent a positive value, and only one addition. Finally, 0 + 0. It's easier to see with parenthesis : System.out.println(i + (+(+(+(+(+(+(+i))))))));


[My Blog]
All roads lead to JavaRanch
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
Thanks Chris!

This cleared my doubt
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

If you remove the spaces, then it will give you a compilation error

i+++++i

this type of statement doesn't give a compilation error in C++, but it gives error in java. so you have to write something like this

i++ + ++i


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Funnily interesting
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
I can see no practical value of such knowledge. If I saw this question on the real exam I'd be out for breaking the legs of the person who set it
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

By the way, I would have chosen "compiler error", if "stupid" was not one of the answers
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple '+' opeartors in a statement