--a

is similar to

a = a - 1;

which means at first it calculates value of a-1 and then with a = ... it assigns that value back to a.

But in case of -a you are just calculating negative value, but it doesn't doesn't reassign it back to a. So since you are not doing anything with that calculated value it will be lost so compiler informs you that your code doesn't do what you may think it does.

Try explicitly assign that result back to a with

a = -a;

After this instruction a will hold new value which you can use anywhere.


This problem disappears when you are using

System.out.println("Your number is: " + (-a) );

since now compiler sees that calculated value -a is being used (as part of value passed to println method).

Answer from Pshemo on Stack Overflow
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java numbers โ€บ increment and decrement unary operators in java
Increment and Decrement Unary Operators in Java
It decrements the value by 1 where --x = x-1. It logically inverts the value of a boolean like if x = true, then !x will be false. The increment (++) operator (also known as increment unary operator) in Java is used to increase the value of a variable by 1.
Published ย  December 5, 2024
Discussions

Pre- and Post-decrement example understanding
i-- is the postfix decrement operator. --i is the prefix decrement operator. The postfix operator decreases the value of i by 1, and then returns the original value. The prefix operator decreases the value of i by 1, then returns the new value. Here is what is happening in your code, expanded to show the order of operations. // Create the variable i with an initial value of 0 int i = 0 // i is now equal to 0 // decrement the variable i and store the original value in a temporary variable, a int a = i-- // i is now equal to -1, a is equal to 0 // drecrement i again and store the new value in a temporary variable, b int b = --i // i is is now equal to -2, b is equal to -2 // set i equal to the difference of the two temporary variables. // i = 0 - -2 i = a - b // i is is now equal to 2 print("result: " + i) More on reddit.com
๐ŸŒ r/learnprogramming
11
4
September 29, 2022
Confusing expression regarding increments and decrements (Java)
8 += 9 + 9 + 7 Left hand side y value is cached as 8. ++y value is incremented to 9 then used y-- value is 9 (from previous increment), then decremented to 8. --y is decremented to 7, and that value is used. Edit: fixed per u/Updatebjarni . More on reddit.com
๐ŸŒ r/learnprogramming
13
1
March 23, 2018
java - Do increment and decrement operators decrease readability? - Software Engineering Stack Exchange
I understand what increment and decrements operators are (++ and --) and the difference between post and pre (i++ vs ++i) but should they be avoided as they do increase the difficulty of reading th... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
November 1, 2016
Java Objects Increment/Decrement
Chris Tapia is having issues with: I'm not sure what I did wrong...I know I need to implement a while loop but I'm unsure of how to do this because unlike the PEZ example, where t... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
June 23, 2015
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ increment-and-decrement-operators-in-programming
Increment and Decrement Operators in Programming - GeeksforGeeks
July 23, 2025 - Increment and Decrement Operators are Unary Operators commonly used in programming to increase or decrease the value of a variable by one, respectively. They provide a shorthand way to perform these common operations. Table of Content ยท Increment Operators ยท Increment Operators in C ยท Increment Operators in C++ Increment Operators in Java ยท
๐ŸŒ
Codefinity
codefinity.com โ€บ courses โ€บ v2 โ€บ 8204075c-f832-4cb9-88b1-4e24e74ebdcb โ€บ 3e046a04-a59c-4fc2-95d5-5d34cff8249b โ€บ d5d1873b-9640-4d8b-94de-d61ed41c8651
Learn Increment and Decrement | Loops
In the second for loop, the variable j is initialized to 5, decremented by 1 after each iteration, and the loop executes until j is no longer greater than 0. This will output the numbers from 5 to 1 in descending order. Java also allows you to simplify expressions using assignment operators.
๐ŸŒ
Oracle
blogs.oracle.com โ€บ javamagazine โ€บ java-increment-decrement-operators
Quiz yourself: Understanding the syntax of Javaโ€™s increment and decrement operators | javamagazine
January 9, 2023 - It might cause some concern that these are applied to the variable i, which is of Integer type. After all, Integer objects are immutable. However, this is not a problem. All that happens is that the contents of the variable are unboxed, the resulting int is incremented or decremented, and then a new Integer is created with that new value.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ pre- and post-decrement example understanding
r/learnprogramming on Reddit: Pre- and Post-decrement example understanding
September 29, 2022 -

Hello All!

I have the following code snipet, which has a result of 2. Can anybody help me why is that? My face is just melting because of this.

int i = 0;
i = i-- - --i;
System.out.println("Result: " + i);

Thank you!

Edit: Guys thank you so much for your explanations!

One thing to note that I have found this syntax in an interview preparing material and I would never use something like this.

Top answer
1 of 5
8
i-- is the postfix decrement operator. --i is the prefix decrement operator. The postfix operator decreases the value of i by 1, and then returns the original value. The prefix operator decreases the value of i by 1, then returns the new value. Here is what is happening in your code, expanded to show the order of operations. // Create the variable i with an initial value of 0 int i = 0 // i is now equal to 0 // decrement the variable i and store the original value in a temporary variable, a int a = i-- // i is now equal to -1, a is equal to 0 // drecrement i again and store the new value in a temporary variable, b int b = --i // i is is now equal to -2, b is equal to -2 // set i equal to the difference of the two temporary variables. // i = 0 - -2 i = a - b // i is is now equal to 2 print("result: " + i)
2 of 5
4
So, first, two warnings: Code like this is bad, don't do it in practice. Only useful for explaining how things work. In several languages, notably C and C++, the result of a line like this is not defined. Since you're using Java, the behavior is defined, but you can't expect an average Java programmers to be able to authoritatively understand what that line does. Anyway, here's what happens, in order. First line: int i =0. We declare a variable, i, and we set its value to zero. Second line. First, there are two expressions to evaluate: i-- and then --i. First we do i--. i's value is zero, so the value of this expression is 0. We'll be evaluating 0 - --i. As a side effect, i's value now becomes -1. Then we evaluate --i. I's value is -1, so we change it to -2, and the value of this expression is -2. We'll be evaluating 0 - -2. 0 - -2 is 2. The line says assign that result to i, so we do. The variable i is now 2. Third line: i's 2, so we print 2. If you think about line two as i = first_expression - second_expression, we could rewrite the whole thing like this: int i=0; int first_expression = i; i = i - 1; i = i - 1; int second_expression = i; i = first_expression - second_expression;
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Increment_and_decrement_operators
Increment and decrement operators - Wikipedia
January 16, 2026 - These examples also work in other C-like languages, such as C++, Java, and C#. Increment operator can be demonstrated by an example: #include <stdio.h> int main() { int c = 2; printf("%d\n", c++); // this statement displays 2, then c is incremented by 1 to 3. printf("%d", ++c); // this statement increments c by 1, then c is displayed. return 0; } ... The following list, though not complete or all-inclusive, lists some of the major programming languages that support the increment and decrement operators.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 753379 โ€บ java โ€บ Decrement-numbers-loop
Decrement of numbers using while loop (Beginning Java forum at Coderanch)
August 2, 2022 - Well, after n has decremented, the loop condition is checked again, it sees that n is still less than 6 (and will be for a loooong time, since we're decrementng n and not incrementing it), so the loop is run again, but this time with n = -1. This continues running for n = -1, n = -2, n = -3, etc. All the time, the sum will become more and more negative, because you're adding negative numbers with an increasing magnitude to it. Java's primitive integral types have this property that they "underflow".
๐ŸŒ
Mathbits
mathbits.com โ€บ JavaBitsNotebook โ€บ Looping โ€บ Increment.html
Increments and Decrements - JavaBitsNotebook.com
increment and decrement operators work only with integer variables -- not on floating point variables and not on literals ยท the execution of the prefix and postfix is under Java control -- nothing you can do will change the order of execution -- parentheses will not force an early execution: ...
Top answer
1 of 5
6

Your question

Yes it's completely ok to use i++ and --j alone in a statement. Most mainstream languages have adopted them.

Yes it's ok to use i++ and --j in more complex expressions, when the precedence is self-explanatory :

  • either because it's isolated enough as in a[i++] or in --j > 0,
  • or because it's used according to a common language idiom like *p++ (experienced C/C++ programmer know that increment applies to the pointer/iterator and not to the dereferenced value).

Additional advise

Avoid in case of doubts! Prefer readability and simpler steps instead of using parentheses as for **p[i]++. Abuse like --(*p++) or 1---j should really be forbidden, not for syntactic ambiguity, but to the huge effort it requires to understand.

Absolute prohibition: Never use more than one such side effect on the same variable in the same expression as in *p++ == *--p or funct(i++, i--) . This might lead at worst to undefined behavior, at best to misunderstandings as there are subtle differences across languages.

Conclusion and quote of the day:

" Debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it ? "

B.W.Kernighan

2 of 5
4

Let's walk through some logic:

  1. Some junior developers don't fully understand these statements
  2. So we avoid using them and limit their exposure to such statements
  3. Eventually all developer will encounter such a statement
  4. They misunderstand the statement because of #2

In a nutshell, when statements are avoided because they are considered esoteric they become more esoteric. The problem is that if these things are part of the language, they will be found in code somewhere. You might be able to prevent their use on a team but in a way you creating ignorance in doing so.

This problem is why I am not a big fan of "everything plus the kitchen sink" languages. The more syntax there is, the more there is to understand and if some of it is kind of cruddy or awkward, it becomes esoteric and eventually causes a bug somewhere when it is misunderstood. Adopting such a language and then declaring that people shouldn't use the syntax it offers isn't really optimal, in my opinion. Python doesn't allow ++i and i++ and if you hate these statements, that's an advantage of that language.

In languages that allow these things, my answer is "know your tools". These statements are well defined, quite logical, and are useful in common situations. If you are programming in a language that allows these statements and don't know what they mean, you need to skill-up and it's not the responsibility of other developers to coddle you.

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ a guide to increment and decrement unary operators in java
A Guide to Increment and Decrement Unary Operators in Java | Baeldung
February 17, 2025 - In Java, the increment unary operator increases the value of the variable by one while the decrement unary operator decreases the value of the variable by one.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-increment-and-decrement-operators-in-Java-with-a-few-examples-and-programmes
What is the use of increment and decrement operators in Java with a few examples and programmes? - Quora
Answer (1 of 4): The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. [code]x = x+1; is the same as x++; And x = x-1; is the same as x--; [/code]Pre incrementing or pre decrementing or ...
๐ŸŒ
Programiz
programiz.com โ€บ article โ€บ increment-decrement-operator-difference-prefix-postfix
Increment ++ and Decrement -- Operator as Prefix and Postfix
In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ java-objects-incrementdecrement
Java Objects Increment/Decrement (Example) | Treehouse Community
June 23, 2015 - I'm not sure what I did wrong...I know I need to implement a while loop but I'm unsure of how to do this because unlike the PEZ example, where there is a while declaration in Example file, this canvas only has GoKart.java.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 430244 โ€บ increment-and-decrement-operators-in-java-and-c
Increment and Decrement operators in Java and C++? | Sololearn: Learn to code for FREE!
here the evaluation is 0 + 0 in c++ which results in zero. if you consider this code int a=0; cout<<(++a + ++a +a++); it will print 6 remember in c++, the evaluation is from right to left. here the evaluation is 3+3+0 that is pre-increment takes the final value and post-increment takes the value at that instant and then increments it. but in Java, the answer is 5. Here in Java, the evaluation is done from left to right here the evaluation is 1+2+2 that is pre-increment first increments the value and then takes the value and post-increment first takes the value and then increments it. the main difference in java and c++ 1)direction in which the evaluation is done. 2)pre-increment and pre-decrement operators.In c++, PI and PD take the final value of the variable and in Java, PI and PD take the value at that instant.
๐ŸŒ
DEV Community
dev.to โ€บ paulike โ€บ increment-and-decrement-operators-564k
Increment and Decrement Operators - DEV Community
April 24, 2024 - The increment operator (++) and decrement operator (โ€“โ€“) are for incrementing and decrementing a... Tagged with learning, programming, java, beginners.