-2

What is the difference between (++c) and (c++)?

Lets say c = 4

I know that for (++c) you would increment increment 4 by 1 so 5, but for (c++)?

EdtheBig
  • 161
  • 1
  • 2
  • 7

1 Answers1

1

Both c++ and ++c increment the variable they are applied to. The result returned by c++ is the value of the variable before incrementing, whereas the result returned by ++c is the value of the variable after the increment is applied.

example:

public class IncrementTest{
public static void main(String[] args){

    System.out.println("***Post increment test***");
    int n = 10;
    System.out.println(n);      // output  10
    System.out.println(n++);    // output  10
    System.out.println(n);      // output  11

    System.out.println("***Pre increment test***");
    int m = 10;
    System.out.println(m);      // output  10
    System.out.println(++m);    // output  11
    System.out.println(m);      // output  11
}
}

For more info, read this: http://www.javawithus.com/tutorial/increment-and-decrement-operators Or google post increment and pre increment in java.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • 3
    Actually, this is misleading. `c++` and `++c` are expressions that return a value. It is not 2 statements. `c++` just returns the _previous_ value when `++c` returns the _updated_ value. You should link the relevant part of the Java Language Specification: it is very clear on this topic (e.g. [15.14.2](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.14.2)). – Tunaki Jan 30 '16 at 23:29
  • 1
    I noticed you edited to put "expression" but it is still misleading. There is no "First the expression is executed then increase the value by one". `c++` is the expression. `c` is incremented by 1. And that expression returns a value, which is the previous value of `c`. – Tunaki Jan 30 '16 at 23:36
  • @Tunaki Let me think for a second what the objection is. Give me a second please. – Faraz Jan 30 '16 at 23:37
  • I think you should yes. The second paragraph is fine now. – Tunaki Jan 30 '16 at 23:44
  • 2
    Well, you still have that notion of "first... then" which is incorrect: "first evaluate the expression then increase the value by one". I think you should read carefully the specification about this to clear your head :). It reads easily: [Postfix Increment Operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.14.2) and [Prefix Increment Operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15.1) – Tunaki Jan 30 '16 at 23:54
  • @Tunaki : I read it 4-5 times. I "think" I got it. What I think my first step is actually the 2nd step. And my 2nd step is completely wrong. The actual step (3rd) is: "...and the sum is stored back into the variable." Am I correct now? – Faraz Jan 31 '16 at 00:06
  • 1
    That depends what you call "step". Technically, it's instructions on the byte-code level. The first step would be "increment `c`" and the second step would be "return the value". It's that second step that varies (the value before or after the increment) – Tunaki Jan 31 '16 at 00:18