Increment/Decrement operators - simple question

Discussion in 'Java' started by edtechworld, Mar 31, 2009.

  1. edtechworld

    edtechworld New Member

    Joined:
    Mar 31, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This is very elementary -- but kindly explain -- can someone please explain why the output of the code below is this:

    OUTPUT:
    a = 2
    b = 3
    c = 4
    d = 1

    How did a = 2 as above? Thanks.

    Code:
    public class MainClass {
       public static void main(String args[]) {
        int a = 1;
        int b = 2;
        int c;
        int d;
        c = ++b;
        d = a++;
        c++;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
      }
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice