0
what is the difference between a++ and ++a? they are integer, thought.
2 Antworten
+ 10
Quoting an old post about 'Increment ++ and Decrement -- Operator as Prefix and Postfix'
In programming (Java, C, C++, PHP etc. ), increment ++ operator increases the value of a variable by 1 and decrement -- operator decreases the value of a variable by 1.
Suppose, a = 5 then,
++a; //a becomes 6
a++; //a becomes 7
--a; //a becomes 6
a--; //a becomes 5
Simple enough till now. However, there is a slight but important difference you should know when these two operators are used as prefix and postfix.
++ and -- operator as prefix and postfix
Suppose you use ++ operator as prefix like: ++var. The value of var is incremented by 1 then, it returns the value.
Suppose you use ++ operator as postfix like: var++. The original value of var is returned first then, var is incremented by 1.
e.g
Suppose a = 5,
System.out.println(a++);
// outputs 5
// but after printing, value stored in a is now 6
System.out.println(a);
// outputs 6
System.out.println(++a);
//outputs 7
+ 2
this question has been posted a hundred times. use the search functionality in the Q&A section, you'll end up with a ton of useful information