• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple '+' opeartors in a statement

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))))))));
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris!

This cleared my doubt
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funnily interesting
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I would have chosen "compiler error", if "stupid" was not one of the answers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic