subtraction - Java: Prefix - Postfix issue -


i have small issue performing subtraction on numbers using prefix , postfix operators. program:

public class postfixprefix   {     public static void main (string args[])     {       int = 5;       int b;       b = (a++) - (++a);       system.out.println("b = " +b);     }   }   

doing this, theoretically supposed 0 answer, however, getting -2.

when try individually try increment values in program:

public class postfixprefix   {     public static void main (string args[])     {       int = 5;       int b, c;       b = a++;       c = ++a;       system.out.println("b = " +b);       system.out.println("c = " +c);     }   } 

i values b = 5, c = 7.

so idea 'c' takes value of 'a' 'b' (please correct me if wrong), want know is

  1. how can have not take value of 'a' 'b', and
  2. using prefix - postfix, can have 0 answer when they're subtracted.

b = a++; means:

  1. assign b value of a
  2. increase 1

c = ++a means:

  1. increase 1
  2. assign c value of a

b = (a++) - (++a) means:

  1. get value of (5) (a without ++)
  2. increase value of 1 (thus making 6) (the result of a++)
  3. increase 1 (++a) (thus making 7)
  4. assign b thae value 5-7=-2 (5 step 1, 7 step 3)

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -