what does it mean when there is a --
or ++
before or after a term like for example Array[1] = Array[size--];
or int position = ++size;
Asked
Active
Viewed 4,083 times
-3
-
6Read about unary operators: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html – Mark Leiber Apr 21 '15 at 17:12
-
I cannot believe this kind of question has a dup. – Luiggi Mendoza Apr 21 '15 at 17:32
-
People in the answers are leaving something relevant out so I'll mention it here. Where the unary operator is **matters**. If it's before, i.e ++size, then it increments **before** size is used. If it's after, i.e size++, then it increments **after** size is used. – Nicholas Eason Apr 21 '15 at 17:54
-
@NicholasEason that should be in an answer, not in a comment. – Luiggi Mendoza Apr 21 '15 at 17:55
-
@LuiggiMendoza It's not an answer to the question though, it's just a fact related to the question. With your logic, Mark should make that link an answer. – Nicholas Eason Apr 21 '15 at 17:57
-
We should not provide link-only answers. If you provide an answer, it should be with your own words and you may refer to content in external links without plagiarism. And your comment seems more like a half-answer and it should be there. – Luiggi Mendoza Apr 21 '15 at 17:58
2 Answers
3
If you add ++
or --
to the beginning of a variable then whenever your program runs and sees that particular code it will either increment, or decrement your variable by 1 before the rest of that line.
Ex:
int num = 20;
System.out.println(--num) // Changes num to 19 first, then prints 19 to the console
Alternatively, adding '++' or '--' to the rear of a variable will do the code after everything else.
Ex:
int num = 20;
System.out.println(num++) // Prints 20 to the console, then changes num to 21
System.out.println(num) // This would now print 21

Lailanater56
- 31
- 4
-2
It mean's that the values are counted up or down
EDIT: to correct my failure:
size++;
is equal to size = size + 1;
and
size--
is equal to size = size -1;

CodeFox
- 447
- 4
- 12
-
-
Not exactly. Both `(size - 1) == (size--)` and `(size--) == (size - 1)` are false. – Andy Turner Apr 21 '15 at 17:15
-
and what does Array[1] = Array[size--]; mean? we put what into array[1]? array[before the last element ]? – ough lala Apr 21 '15 at 17:16
-
2@oughlala Dude, **read the link** Mark provided you in his comment to your OP. You can also just try it yourself and find out. I suggest both. – tnw Apr 21 '15 at 17:24
-
@AndyTurner how would they evaluate to false? Are they not both the same value? – Ungeheuer Apr 21 '15 at 17:39
-
"and `size++;` is equal to `size + 1;`" you probably meant `size = size + 1;`. Same about `--`. – Pshemo Apr 21 '15 at 17:42
-
@JohnnyCoder They're not. E.g. if size was 3, they would be `2 == 3` and `3 == 1`. – Bubletan Apr 21 '15 at 17:42
-
but it's `size--` which would make `size = 2` and `size - 1` would decrease `size at that specific moment and then the change would be lost because it is not stored. `size++` would increase `size` making it `size = 4` and `size + 1` would also increase `size` but just for that moment and would be lost as the change was not stored. – Ungeheuer Apr 21 '15 at 17:54