Pages

Friday, February 15, 2013

Increment and Decrement Operators in Java



Operators are of various types in Java and of them increment and decrement operators are special ones with much more complex functionality with pre and post types. In this post, you will understand various ways on how these operators perform depending on the usage.


Increment and Decrement are divided into Pre and Post i.e.,

a. Pre-Increment and Post-Increment
b. Pre-Decrement and Post-Decrement

Pre and Post Increment Operators (++) :

If we use ++ before the variable then it is Preincrement and if we use it after the variable it is Postincrement

int a=10;
++a;//pre increment - First increments the value and then prints it.
a++;//post increment - Incremements the value after printing it.

Example Program : http://pastie.org/6170554

Pre and Post Decrement Operators (--) :

If we use ++ before the variable then it is Preincrement and if we use it after the variable it is Postincrement

int a=90;
--a;//pre decrement - First decrements the value and the prints it
a--;//post decrement - First prints the value and then decrements it

Example Program : http://pastie.org/6170555

0 comments:

Post a Comment