Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

++ operator interesting problem

 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys !!! need your help with the basic ++ operator problem.
This is with reference to ivor horton's book "Beginning java", where he teaches us about the ++ operator in chapter 2.

we have 3 variables, numapple, numfruit, numorange.
let us say numapple=9 and numorange=7;
numfruit = numapple + numorange;
which yields 16 as answer.

if i write numfruit = numapple + ++numorange;
the answer becomes 17, which is correct.

Now, if i write numfruit = numapple+++numorange why do you think the answer comes out to be 16 again and not 17. what is the logic behind it. i know we can use paranthesis or spaces and stuff, but what do you all think is the logic behind it.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
numapple+++numorange is actually (numapple++)+numorange. numapple++ first evaluates to 9, then increases numapple to 10. The result is therefore 9+7=16.
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS ROB, but my question still remains, what is the logic behind it, it's internal implementation, or how the compiler sees it.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial covering operators could be helpful here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

As you can see from the operator precedence table, the unary ++ operator preceeds additive + operator.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting!

So only because of Precedence,


is converted to


and not to the


Right?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right
 
Phuc Bui
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i write numfruit = numapple+++numorange why do you think the answer comes out to be 16 again and not 17.

it's interesting
So paranthesis should be used to avoid misunderstanding

Cheers,

Phuc Bui
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spaces would be enough in this case. The spaces tell the compiler what +++ means: either + ++ (addition and pre-increment) or ++ + (post-increment and addition). In fact, for readability, I think it's always better to put spaces around any operator.

But you are right: if you ever get an unexpected result for a term, try adding parentheses - most likely you misinterpreted the precedence rules.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used to code in C++ in my school days and we use to create programs like this for fun

a+++++b

now the C++ compiler interpreted it as

(a++) + (++b)

but the java compiler rejects this syntax (a+++++b) and gives a compilation error . Using spaces as Rob said solves the problem...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote: the java compiler rejects this syntax (a+++++b) and gives a compilation error
Thank goodness
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java compiler isn't that smart sometimes. Consider the following:

For a developer it is quite clear what most mean (a+++b could be considered as a + ++b as the only exception), yet the compiler doesn't understand. That's probably because both ++ and + are operators, and the scanner part is greedy - it will find ++ as one operator, not + and + as two.
So the compiler probably sees these statements as this:

Therefore, like I already said, add spaces around your operators. It will make it easier to read, and easier for the compiler to know what you mean. Also, the chances of any ambiguity for the developer (as in a+++b) will be erased.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting ! Rob Rocks
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:Interesting ! Rob Rocks


+1
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The Java compiler isn't that smart sometimes.

If you submit this kind of examples to SUN is there any scope of improvement? I think interpreter does this job so there might be some scope of improvement.
Or SUN has provided documentation about this and we are supposed to accept it and live the life?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically it should be possible to change the compiler so none of these examples will give a compiler error. However, it most likely requires rewriting of several grammar rules, and as such will take a lot of work, and even more testing (since basically the entire compiler changes).
I doubt Sun will put that much effort into it for something that can easily be prevented by adding a few spaces - something that is a good recommendation anyway, even if it wouldn't be necessary to prevent compiler errors.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I doubt Sun will put that much effort into it for something that can easily be prevented by adding a few spaces - something that is a good recommendation anyway


Rightly said. I don't think most people will like this syntax "a+++++b" (right Campbell )
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Rightly said. I don't think most people will like this syntax "a+++++b" (right Campbell )
I hope not!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic