This is most useful and confusing operator in java or any programming language. It is used to increase and decrease the value of a variable by 1. It is a Unary operator but the operand must be a variable containing numeral value and should not be a constant because constant cannot be modified in java i.e the final variable in java as a constant and direct value act as a constant.

for ex-
int    x  =  ++10 ;  // Invalid because 10 is a constant here. 

int    x  =  10;
x++  ; 
Above code is valid because here it increments a variable but (x++)++ is invalid because first it increments a variable then second time it contain a incremented value so it act like a constant and hence cannot be incremented further.

int    x  =  10 ;
(x++)++  ;     // This statement is equal to (10)++ which increments a constant value.
++(++x)  ;    // This statement is equal to ++(11) which increments a constant value.

Increments operator ( ++ ) 

This operator is used to increase the value of a variable by 1. Again it of two form i.e Post increment and Pre increment.

Post-fix operator : It is used to increment the value by one but in the next step when the variable is used again in our program . Currently it will always hold the current value but increment in the next step only, that is why it is named as post.
If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  10;
x++ ;               // In this  step it will hold the current value of x i.e 10.
System.out.println ( " Incremented value : " + x) ;  // If we try to print or use " x " then it will print or use incremented value.

Pre-fix operator : It is used to increment the value by one in the current step only that is why it is named as pre. If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  10 ;
++x ;                // In this  step it will hold the incremented value of x i.e 11.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use the previously incremented value. 

Now you must be wonder to see that both are having the same output then what is the difference. Hole on and see this example, will make everything clear about post and pre.

int    x  =  10;
int    y  =  10;
System.out.println(" Incremented value : " + (x++)) ; 
System.out.println(" Incremented value : " + (++y)) ; 

Now try it execute the above statement it will show you the output as 10 and 11. Now you can conclude that post will first hold the value and then increment it where as pre will increment the value at the same time.

int    x  =  10;
System.out.println(" Incremented value : " + (x++)) ; 
System.out.println(" Incremented value : " + (++x)) ; 

Here the output will be 10 and 12 at the first print step it will hold the current value i.e 10 and then in the in step it will increment it 11 and then it will increment to 12 because here in the 2nd print statement there is a pre increment which make it to 12.
Hence 11 because of post and then 12 because of pre increment is used.

Decrements operator ( ++ )  

This operator is used to decrease the value of a variable by 1. Again it of two form i.e Post decrements and Pre decrements

Post Decrements : It is used to decrements the value by one but  in the next step when the variable is used again in our program. Currently it will always hold the current value but decrements in the next step that is why it is named as post.
If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  20 ;
x-- ;                  // In this  step it will hold the current value of x i.e 20.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use Decremented value i.e 19

Pre Decrements : It is used to decrements the value by one in the current step, that is why it is named as pre.
See the following example to clear out all your doubts.
for ex-

int    x  =  10;
--x ;                  // In this  step it will hold the incremented value of x i.e 9.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use the previously decremented value. 

No need to wonder because here it is working same as it was in increments operator.You will see the same difference but the difference is that here value will get decreased. See the following example.

int    x  =  10;
int    y  =  10;
System.out.println(" Incremented value : " + (x--)) ; 
System.out.println(" Incremented value : " + (--y)) ; 

Now try it execute the above statement it will show you the output as 10 and 9. Now you can conclude that post will first hold the value and then increment it where as pre will increment the value at the same time.

int    x  =  10;
System.out.println(" Incremented value : " + (x--)) ; 
System.out.println(" Incremented value : " + (--x)) ; 

Here the output will be 10 and 8 at the first print step it will hold the current value i.e 10 and then in the in step it will increment it 9 and then it will increment to 8 because here in the 2nd print statement there is a pre increment which make it to 8.
Hence 9 because of post and then 8 because of pre decrements is used.

Now try to execute more using more increment operator in a single steps like

int  y  =  10 ;
y+++(++y)+(++y)+(++y) ; 

It will give output as 49 think why ans try to find which way it is executing whether it is executing form left to right or right to left.

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic