Increment and Decrement Operator

By: aathishankaran Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Increment and Decrement Operator


            The ++ and the – are java’s increment and decrement operators. They were introduced in previous article. Here they will be discussed in detail. As you will see, they have some special properties that make them quite interesting. Let’s begin by reviewing precisely what the increment and decrement operators do.


            The increment operator increases its operand by one. The decrement operator decreases its operand by one. For example, this statement:


             x = x + 1;


can be rewritten like this by use for the increment operator:




            Similarly, this statement:


     x = x –1;


is equivalent to




These operators are unique in that they can appear both in postfix form, where they follow the operand as just shown, and prefix form, where they precede the operand. in foregoing examples, there is no difference between the prefix and postfix forms. However, when the increment and / pr decrement operators are part of a larger expression, then a subtle, yet powerful, difference between these two forms appears. In the prefix form, the operand is incremented or decremented before the value is obtained for use in the expression. In postfix form, the previous value is obtained for use in the expression, and then the operand is modified. For example:


            x = 42;

            y = ++x;


In this case, y is set to 43 as you would expect, because the increment occurs before x is assigned to y. thus, the line y=++x; is the equivalent of these two statements:


            x = 42;

            y = x++;


The value of x is obtained before the increment operator is executed, so the value of y is 42. Of course, in both cases x is set to 43. Here, the line y = x++; is the equivalent of these two statements:


            y =x;

            x = x + 1;


The following program demonstrates the increment operator.


// Demonstrate ++.

class InDec {

     public static void main (String args[]) {

          int a = 1;

          int b = 2;

          int c;

          int d;

          c = ++b;

          d = a++;


          System.out.println(“a = “ +  a);

          System.out.println(“b= “ +  b);

          System.out.println(“c = “ + c);

          System.out.println(“d = “ + d);




The output of this program follows:


     a = 2

     b = 3

     c = 4

     d = 1



Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

i really have to thank you for the wonderful fluidity in explaining this really helped me a lot...thank you

View Tutorial          By: Joel Pereira at 2011-03-19 22:08:58
2. View Comment

int n = 0;
for (int m = 0; m < 5; m++) {
n = n++;



View Tutorial          By: max at 2011-05-26 09:41:26
3. View Comment

this is because dear u r using here postfix increment n++ this postfix assigned value 0 to n always before the increment in value and result would b 0 0 0 0 0 throughout the loop if u want that ur output will 1 2 3 4 5 then use prefix ++n here.

View Tutorial          By: sajid at 2011-09-06 20:51:54
4. View Comment

sir, in this example,which value of n is being stored in the memory location?
i mean its n=n++;
in 1st iteration (l.h.s)n=0 and (r.h.s)(n=n+1 after its usage (n=0)by n)
shouldnt the incremented value of n be printed in every iteration???

View Tutorial          By: shivom at 2012-01-15 07:16:49
5. View Comment

j = 0;
i = 0;

CAN I PLEASE know how this loop works and if it does , how many times will it loop. i and j are 8 bits each.

View Tutorial          By: alistair at 2012-01-28 06:05:48
6. View Comment

Answer for Comment 4 :

" n=n++ ; " is a useless line !! it doesn't do anything. what's actually happening there is, that the value of n was assigned 0, So, when you run the loop the value of n++ gets stored in n (as we had n=n++) which is 0 (as a post increment operator first reads the value then increments 1to it). So the value of n++ is 0 which is being stored by n . *output comes 0 for the first loop and as following by all other loops output is 0 till the end*

In simple way !!

int n=0;
for(int m=0 ; m<5 ; m++){
n=n++ //n++ == 0 at this time so, n becomes 0

To avoid this useless thing instead of using
n++; //This would add 1 in each loop :)

Thank you . I hope now you don't have any doubt on "n=n++;" ;)

View Tutorial          By: NewB_of_java at 2012-02-16 16:52:05
7. View Comment

Dude, that LOOP wont work i cant even understand what you are trying to do the loop cant go negative, get some brain, i mean "--j" ?? What was that ?? xD

View Tutorial          By: Rohan at 2012-02-16 17:04:06
8. View Comment

class Evaluate
public static void main(String args[])

int y=6;
int x=++y+2y;
System.out.println("The value is:"+x);
catch(Exception e)
i need to know how this is incorrect......

View Tutorial          By: prajjawal at 2013-02-27 12:36:58
9. View Comment

this code is running successfully

View Tutorial          By: Amit Agarwal at 2013-03-08 10:56:39
10. View Comment

Answer for comment 5
this loop is work infinitely until the variables (used in loop) have compete their range.

View Tutorial          By: salman at 2013-03-10 07:30:53
11. View Comment

is it possible to change the increment variable?

View Tutorial          By: Lundi at 2013-04-08 21:21:13
12. View Comment

why java from two to the power 7 bit was upgraded to two to the 8 bit

View Tutorial          By: avinash at 2013-05-28 08:34:24
13. View Comment

COOL...........Nice ,understanding .simple i like it man $$$$$$

View Tutorial          By: DON at 2014-06-12 16:48:46
14. View Comment

what will be the answer of this?
int y=10;
int z=(++y*(y+++5)

View Tutorial          By: divye at 2015-03-20 09:05:46

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
InetAddress Example program in Java
Read from a COM port using Java program
Stack example in Java - push(), pop(), empty(), search()
How to use ArrayList in Java
The Basic Structure of a Simple Java program
Using StringTokenizer in Java
FileReader and FileWriter example program in Java
Transient vs Volatile modifiers in Java
Vector example in Java
Method Overriding in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
append() in Java
Student marks calculation program in Java
Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
Text to Speech conversion program in Java
inheritance in Java
How to initialize an Array and how to copy the array
Using Charts in JasperReports
What is Unicode?
Operator Precedence in Java
Of Object, equals (), == and hashCode ()
DateFormat sample program in Java
A Simple whois program in Java
PushbackReader sample program in Java
CharArrayReader example program in Java
ByteArrayOutputStream - sample program in Java
Vector example in Java
Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()
HashSet Sample program in Java