- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the restrictions on increment and decrement operators in java?
The increment operator increments the value of the operand by 1 and the decrement operator decrements the value of the operand by 1. We use these operators to increment or, decrement the values of the loop after executing the statements on a value.
Example
public class ForLoopExample { public static void main(String args[]) { //Printing the numbers 1 to 10 for(int i = 1; i<=10; i++) { System.out.print(" "+i); } System.out.println(" "); //Printing the numbers 10 to 1 for(int i = 10; i>=1; i--) { System.out.print(" "+i); } } }
Output
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
Restrictions on increment and decrement operators
- You cannot use increment and decrement operators with constants, if you do so, it generates a compile time error.
Example
public class ForLoopExample { public static void main(String args[]) { int num = 20; System.out.println(num++); System.out.println(20--); } }
Output
ForLoopExample.java:5: error: unexpected type System.out.println(20--); ^ required: variable found: value 1 error
- You cannot nest two increment or decrement operators in Java, if you do so, it generates a compile time error −
Example
public class ForLoopExample { public static void main(String args[]) { int num = 20; System.out.println(--(num++)); } }
Output
ForLoopExample.java:4: error: unexpected type System.out.println(--(num++)); ^ required: variable found: value 1 error
- Once you declare a variable final you cannot modify its value. Since increment or, decrement operators changes the values of the operands. Usage of these operators with a final variable is not allowed.
Example
public class ForLoopExample { public static void main(String args[]) { final int num = 20; System.out.println(num++); } }
Output
ForLoopExample.java:4: error: cannot assign a value to final variable num System.out.println(num++); ^ 1 error
- Usage of increment and decrement operators with boolean variables is not allowed. If you still, try to increment or decrement a boolean value a compile time error is generated.
Example
public class ForLoopExample { public static void main(String args[]) { boolean bool = true; System.out.println(bool++); } }
Output
ForLoopExample.java:4: error: bad operand type boolean for unary operator '++' System.out.println(bool++); ^ 1 error
- Related Articles
- Increment and decrement operators in Java
- What are increment (++) and decrement (--) operators in C#?
- Python Increment and Decrement Operators
- Increment and Decrement Operators in Python?
- Increment ++ and decrement -- Operators in C++
- Increment and Decrement Operators in C#
- Interesting facts about Increment and Decrement operators in Java
- PHP Increment/Decrement Operators
- Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
- What are the restrictions placed on method overloading in java?
- Increment ++ and Decrement -- Operator Overloading in C++
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- Pre-increment (or pre-decrement) in C
- What are the restrictions on creating a vector in R?
- What are Lob Data Types? What are the restrictions on these datatypes in JDBC?

Advertisements