Home   |   About   |   Terms   |   Contact    
Read & Learn
 

Increment and Decrement operators in Java.

Java

☯ All Java Articles     ☯ All Articles


A new product.






Popular Google Pages:




This article is regarding
Increment and Decrement operators in Java.
Advantage of Increment and Decrement operators in Java.

Last updated on: .



◕ Increment and Decrement operators in Java.

Let have an example first:
int redballs = 5;
++redballs;

This ++redballs is equivalent to the following statement:
redballs = redballs +1;

This means add one with the value of the variable redballs and put the value again into the variable redballs.
Now the value of the redballs is 6.

This ++ sign is called the Increment operator.

And similarly
This - - sign is called the Decrement operator.
Because it decease the value of a variable by one.



◕ One important thing to note that we can put this Increment or Decrement Operator before or after a variable.
Example:
++redballs;
redballs++;

These two has the different meaning as follows.

■ 1st example:
int redballs = 5;
int blueballs = 3;
int numballs = 0;
numballs = ++redballs + blueballs;

Here the result will be 9.
Because the process will be like:
- It will increase the value of the redballs first by 1.
- Then it will add redballs with blueballs.
- And will put the result on the variable numballs.
Hence the result will be 9.

■ Now look at the other example:
int redballs = 5;
int blueballs = 3;
int numballs = 0;
numballs = redballs++ + blueballs;

Here the result will be 8.
Because the process will be like:
- It will not increase the value of the redballs first.
But it will increase the value of the redballs after the execution of the mathematical statement
numballs = redballs++ + blueballs;.
Hence the result will be 8. But after execution of the statement the value of the redballs will be 6.

■ Please always remember one thing, don't put the above statement like this:
numballs = redballs+++blueballs;
Because it will make a confusion as per the following:
numballs = redballs+ ++blueballs; or
numballs = redballs++ +blueballs;
To avoide this we can use parentheses.



◕ The great advantage of the Increment and Decrement Operator.
A great advantage of the Increment and Decrement operators is that we can use them in any expression directly.
Example:
numballs = ++redballs + blueballs;
As We have already seen.



Popular Google Pages:



Related Java articles:
What is Java? Where we can apply Java?
Why Java is a Machine Independent Program? What is Machine Independent Program?
Root Directory of Java
How to Compile and run a Java Program?
What is .class file in Java?
What is variable in a program?
What is identifier in Java?
Integer data types in Java?
What is Primitive Types in Java?
How to declare constant variables in Java?
Why should we declare the initial value of a variable in Java?
Use of Underscores to write an integer in Java




Top of the page

Amazon & Flipkart Special Products

   


Top of the page