1

I am wondering with post and pre increment and decrement operation.

what I know in Java precedence of post operator is high and associativity is left-to-right.while associativity of pre operator is right-to-left

enter image description here

Oracle Java Tutorial

but my code showing me undesired result-

public class Bunnies { 
    static int count = 3; 
    public static void main(String[] args) { 

        System.out.println(--count*count++*count++);//out put is 12 expected 48
        //associativity of post is higher so should be evaluated like this-

        //--count*3**count++  count is 4 now
        //--count*3*4         count is 5 now
        //4*3*4=48


        count = 3;
        System.out.println(--count*++count*++count); //out put is 24 expected 120
        //associativity of pre  is right to left so should be evaluated like this-

        //--count*++count*4      count is 4 now
        //--count*5*4            count is 5 now
        //4*5*4=120


        count = 3;
        System.out.println(-- count*count++);// out put is 4 expected 9

        //--count*3      count is 4 now
        //3*3=9 
         }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • I am checking how these `post` and `pre` works.not a part of real time appliaction.I am sound in `C` with these operator buy wondering me in java. –  May 27 '15 at 09:40
  • You should trust, and cite, the Java Language Specification over any other documentation whatsoever, yea unto and including the Java Tutorial. – user207421 May 27 '15 at 09:47
  • When you have --Number , then the number will decrease first then perform the next operation. And when you have Number++, then the the operation will be performed then the Number will be increased – Allloush May 27 '15 at 09:47
  • The figure is for C++, not Java... – Anders R. Bystrup May 27 '15 at 09:48
  • @ِAllloushI am following oracle documentation https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html –  May 27 '15 at 09:49
  • @AndersR.Bystrup this is done in java plz have a look https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html –  May 27 '15 at 09:50
  • I understand that, but why mix a C++ table into the question without a comment or tag? – Anders R. Bystrup May 27 '15 at 09:51
  • I am using jdk 1.7 and following oracle documentation of java,I used this image by googling but my result also varries if I follow only oracle documentation.@AndersR.Bystrup –  May 27 '15 at 09:54

4 Answers4

3

Order of evaluation of subexpressions is independent of both associativity and precedence.

The subexpression of the multiplication are evaluated from left to right, so when doing --count*count++*count++, you evaluate --count then count++ and finally count++.

And as the pre operator is evaluated first, --count will be decremented before its evaluation. In the same way, as the post operator is evaluated lately, count++ will be incremented after its evaluation.

The precedence only help the compiler to create a correct abstract syntactic tree.
For example, when doing ++count*2, the compiler use the precedence to know the expression is (++count)*2 and not ++(count*2). In the same way, when doing ++count*count--, the expression is (++count)*(count--) and not (++(count * count))-- or whatever. But then, during the evaluation of the multiplication ++count is evaluated before count--.

Hope this help you :)

I just found a great answer about expression evaluation in C# and Java here, enjoy :)

Community
  • 1
  • 1
NiziL
  • 5,068
  • 23
  • 33
  • 1
    role of multimlication come only after `pre`and `post` since they are higher in precedence,this is precedence all about not helpful in query. –  May 27 '15 at 09:58
  • @dubey-theHarcourtians You misunderstand what is the precedence, and confuse it with the evaluation. When doing `++count*2`, the compiler create an AST (abstract syntactic tree) to evaluate the expression, and precedence is used to build this tree. Precedence of pre (or post) operator is higher, so it will be applied before the multiplication, but the multiplication is still evaluated from left to right. See my edit :) – NiziL May 27 '15 at 10:19
  • yes @Nizil there is no matter either * evaluated r-l or l-t result will be same.but the values available for multiplication will evaluated on the basis of `post` and `pre` operator. –  May 27 '15 at 10:24
  • @dubey-theHarcourtians Take a look at [this answer](http://stackoverflow.com/a/6801431/1498389) :) – NiziL May 27 '15 at 10:34
  • this does not mean that we are using multiplication so rest values evaluated l-r.u can easily understand that bcz almost all the operator have l-r associativity so what does it mean pre operator has r-l associativity they always comes with any operator if u r using more than 1 in a single statement,then associativity of r-l will me meanning less pre operator,also this will become impossible to find a single example which can shoe r-l associativity of pre operator –  May 27 '15 at 10:34
  • the link you suggested is desired since associativity of `=`is right to left. –  May 27 '15 at 10:38
1
System.out.println(--count*count++*count++);

= 2 * 2 * 3 = 12

count = 3;
System.out.println(--count*++count*++count) 

= 2*3*4 = 24

count = 3;
System.out.println(-- count*count++);

= 2 * 2 = 4

Pre increment/decrement

++/-- X first increments/decrements then does the operation.

Post increment/decrement

X ++/-- first the operation is done, then increment / decrement.

Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
  • 1
    precedence of post is higher than pre what I mentioned in my question alrady. – –  May 27 '15 at 09:41
  • 1
    `post` operator will evaluated first.why U evaluated `pre` First. –  May 27 '15 at 09:47
  • 1
    The way U shown is fine to match with answer ,could please show the reason why u evaluated pre operator before post operator. – TheCurious May 27 '15 at 10:07
0

I take the first one.

System.out.println(--count*count++*count++);//out put is 12 expected 48
                    2     *    2  * 3 = 12
                    pre   * post  * post
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • 1
    precedence of post is higher than pre what I mentioned in my question alrady. –  May 27 '15 at 09:41
  • @dubey-theHarcourtians It does not matter, since you are using the `*` operator. It stands on his own. – Murat Karagöz May 27 '15 at 09:43
  • K I have never seen logic that u suggested me now -this will not work due to presence of `*` strange! –  May 27 '15 at 09:46
0

count = 3:

Example 1:

--count*count++*count++ equals (--count)*(count++)*(count++)
(--count) = 2
(count++) = 2 (you increment it AFTER you do something with it)
(count++) = 3 ... count was incremented from before
2*2*3 = 12

Example 2:

--count*++count*++count equals (--count)*(++count)*(++count)
--count = 2
++count = 3
2 * 3 * 3 = 24

Example 3:

(--count)*(count++)
--count = 2
2 * 2 (the count++ gets changed afterwards)

Keep in mind that you have to watch the multiplicaton operator

hamena314
  • 2,969
  • 5
  • 30
  • 57
  • 1
    `post` operator will evaluated first.why U evaluated `pre` First. –  May 27 '15 at 09:43
  • I have added some brackets to show which is evaluated when. – hamena314 May 27 '15 at 09:57
  • 1
    role of bracket() is meaning less here since `post` operator and `()` have `same` `precedence`. both of these have highest precedence. –  May 27 '15 at 10:00
  • 1
    `--count*(count++)*(count++)` is fine but you cant put pre operator in bracket by your own.putting in bracket`(--count)` changes my question. –  May 27 '15 at 10:02
  • The brackets are not a notation of programming but of grouping, in order to show which gets executed when. Dont look into your table for the brackets, use the brackets to see the order of evaluation. This is why I have written "equals" (see Nizils answer, he is pointing to the evaluation tree). – hamena314 May 27 '15 at 11:16