| Author |
++ operator interesting problem
|
Kunal Choudhary
Greenhorn
Joined: Feb 25, 2009
Posts: 14
|
|
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
Joined: Oct 27, 2005
Posts: 19214
|
|
|
numapple+++numorange is actually (numapple++)+numorange. numapple++ first evaluates to 9, then increases numapple to 10. The result is therefore 9+7=16.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Kunal Choudhary
Greenhorn
Joined: Feb 25, 2009
Posts: 14
|
|
|
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
Joined: Mar 10, 2008
Posts: 1402
|
|
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.
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Vikas Kapoor
Ranch Hand
Joined: Aug 16, 2007
Posts: 1374
|
|
Interesting!
So only because of Precedence,
is converted to
and not to the
Right?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
Right
|
 |
Phuc Bui
Greenhorn
Joined: Feb 19, 2009
Posts: 26
|
|
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
Joined: Oct 27, 2005
Posts: 19214
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Ankit Garg wrote: the java compiler rejects this syntax (a+++++b) and gives a compilation error
Thank goodness
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
Interesting ! Rob Rocks
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
seetharaman venkatasamy wrote:Interesting ! Rob Rocks
+1
|
 |
Vikas Kapoor
Ranch Hand
Joined: Aug 16, 2007
Posts: 1374
|
|
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
Joined: Oct 27, 2005
Posts: 19214
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
Joined: Oct 13, 2005
Posts: 32599
|
|
Ankit Garg wrote:Rightly said. I don't think most people will like this syntax "a+++++b" (right Campbell  )
I hope not!
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: ++ operator interesting problem
|
|
|