- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Increment Decrement Operator
The operator (++) and the operator (--) are Java's increment and decrement operators. The increment (++) and decrement operator (--) are simply used to increase and decrease the value by one.
The increment operator increases its operand by one and the decrement operator simply decreases its operand by one. For instance, the statement :
x = x + 1;
can be rewritten like this by use of the increment operator i.e., ++:
x++;
Similarly, the statement :
x = x - 1;
is equivalent to:
x--;
These operators are unique in that they can appear both in postfix (where they follow the operand) and prefix (where they precede the operand) form.
Prefix Increment and Decrement Operator
In prefix form, the operand is incremented or decremented before the value is received for use in the expression.
Postfix Increment and Decrement Operator
In postfix form, the previous value is obtained for the use in expression, and then the operand is modified.
Example
Here in the examples, there is no difference between the prefix and the postfix forms. However, when the increment and/or decrement operators are the part of a larger expression, then a subtle, however powerful, difference between these two forms appears. Here is an example:
x = 42; y = ++x;
Here, y is set to 43 as you would expect, as the increment occurs before x is assigned to y. Therefore, the line y = ++x; is equivalent of these two statements:
x = x + 1; y = x;
However, when we written like this,
x = 42; y = x++;
here the value of x is obtained before increment operator is executed, so the value of y is 42. Naturally, in both cases, x is set to 43. Here, the line y = x++; is equivalent of these two statements :
y = x; x = x + 1;
Java Increment and Decrement Operators Example
This Java program demonstrates the increment operator :
/* Java Program Example - Java Increment Decrement Operator * This program demonstrates the increment (++) operator */ public class JavaProgram { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; System.out.println("a = " +a); System.out.println("b = " +b); System.out.println("c = " +c); System.out.println("d = " +d); } }
When the above Java program is compile and executed, it will produce the following output:
Now, this Java program demonstrates the decrement operator :
/* Java Program Example - Java Increment Decrement Operator * This program demonstrates the decrement (--) operator */ public class JavaProgram { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = --b; d = a--; c--; System.out.println("a = " +a); System.out.println("b = " +b); System.out.println("c = " +c); System.out.println("d = " +d); } }
When the above Java program is compile and executed, it will produce the following output:
More Examples
Here are some more examples on increment decrement operators in Java, you can go for:
- Print Fibonacci Series
- Check Palindrome or Not
- Find Armstrong Number
- Generate Armstrong Numbers
- Find ncR nPr
- Print Pascal Triangle
« Previous Tutorial Next Tutorial »