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
- how can have not take value of 'a' 'b', and
- using prefix - postfix, can have 0 answer when they're subtracted.
b = a++; means:
- assign b value of a
- increase 1
c = ++a means:
- increase 1
- assign c value of a
b = (a++) - (++a) means:
- get value of (5) (a without ++)
- increase value of 1 (thus making 6) (the result of a++)
- increase 1 (++a) (thus making 7)
- assign b thae value 5-7=-2 (5 step 1, 7 step 3)
Comments
Post a Comment