Interesting facts about Increment and Decrement operators in Java
Increment operator
It is used to increment a value by 1. There are two varieties of increment operator:
- Post-Increment : Value is first used for computing the result and then incremented.
- Pre-Increment : Value is incremented first and then result is computed.
Decrement operator
It is used for decrementing the value by 1. There are two varieties of decrement operator.
- Post-decrement : Value is first used for computing the result and then decremented.
- Pre-decrement : Value is decremented first and then result is computed.
Interesting facts about Increment and Decrement operators
- Can be applied to variables only : We can apply ++ and — operator only for variables but not for the constant values. If we are trying to apply ++ and — operator on constant value then we will get compile time error.
// Java program to illustrate// Increment and Decrement operators// Can be applied to variables onlypublicclassTest {publicstaticvoidmain(String[] args){inta =10;intb = ++a;// uncomment below line to see error// b = 10++;System.out.println(b);}}chevron_rightfilter_noneOutput:
11
Un-comment to see error:
prog.java:10: error: unexpected type b = 10++; ^ required: variable - Nesting of both ++ and — operators is not allowed
// Java program to illustrate// nesting can not be// applied to ++ and --publicclassTest {publicstaticvoidmain(String[] args){inta =10;intb = ++(++a);System.out.println(b);}}chevron_rightfilter_noneOutput:
error: unexpected type
- For final variables we can’t apply increment and decrement operator : The increment and decrement operators can not be applied to final variables because of the simple reason that their value can not be changed.
// Java program to illustrate// Increment and Decrement operators// Can not be applied to final variablespublicclassTest {publicstaticvoidmain(String[] args){finalinta =10;intb = ++a;System.out.println(b);}}chevron_rightfilter_noneOutput:
error: cannot assign a value to final variable a
- Increment and Decrement Operators can not be applied to boolean : We can apply ++ and — operators for all primitive data types except Boolean.
// Java program to illustrate// Increment and Decrement operators// Can not be applied boolean data typepublicclassTest {publicstaticvoidmain(String[] args){booleanb =false;b++;System.out.println(b);}}chevron_rightfilter_noneOutput:
error: bad operand type boolean for unary operator '++'
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Interesting Facts About Java
- Interesting facts about null in Java
- Interesting facts about Array assignment in Java
- Py-Facts - 10 interesting facts about Python
- Interesting Facts about C#
- Interesting Facts about C++
- Interesting Facts about Linux
- Interesting Facts in C Programming
- Interesting Facts about Ubuntu
- Interesting Facts about Macros and Preprocessors in C
- Interesting facts about Fibonacci numbers
- Interesting facts about strings in Python | Set 1
- Interesting facts about data-types and modifiers in C/C++
- Interesting facts about strings in Python | Set 2 (Slicing)
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array



