Home Java Beginners Auto Increment and Decrement

 
 

Share on Google+Share on Google+

Auto Increment and Decrement

Advertisement
In this example we are demonstrating the significance of prefix and postfix operators by incrementing and decrementing the value of a variable that how both are different with each other.

Auto Increment and Decrement

     

In this example we are demonstrating the significance of prefix and postfix operators by incrementing and decrementing the value of a variable that how both are different with each other. 

Description of program:

To demonstrate the difference between prefix and postfix operator we are taking an example. In this example first of all, we are taking an integer type variable taking 5 as its value and displaying the original value of this variable. Now we are applying the prefix increment operator and display the value on the console and then applying the postfix increment operator and display the value on the console. After that we are applying the prefix decrement operator and display the value on the console and then applying the postfix decrement operator and displaying the value on the console.

	public class AutoInc {

    public static void main(String[] args) {
    
        int a = 5;
        System.out.println("Original value of a : " + a);
        System.out.println();

        System.out.println("After using pre-increment value of a : " + ++a); 
        System.out.println("After using post-increment value of a : " + a++);
        System.out.println();

        System.out.println("After completing post-increment operation value of a : " + a);
        System.out.println();

        System.out.println("After using pre-decrement value of a : " + --a); 
        System.out.println("After using pre-decrement value of a : " + a--); 
        System.out.println();

        System.out.println("After completing pre-decrement operation value of a : " + a);
    }
}

Here is the output:

C:\Examples>java AutoInc
Original value of a : 5

After using pre-increment value of a : 6
After using post-increment value of a : 6

After completing post-increment operation value of a : 7

After using pre-decrement value of a : 6
After using pre-decrement value of a : 6

After completing pre-decrement operation value of a : 5

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 14, 2008

Related Tutorials

Discuss: Auto Increment and Decrement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com