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.

Related Posts:

  • super keyword in Java Like this in java one more most important keyword is there i.e super. super in Java has a wide use and it is mainly used to call the super class constructor explicitly by the user and implicitly used by the JVM to initialize… Read More
  • Type of inheritance in Java According to OOPS ( Object-oriented concept ) inheritance are classified into 6 category in which java support 5 of them and one is not supported in java i.e multiple inheritance for which there is a concept of interface . I… Read More
  • Inner Class in Java Inner class in a class defined as a class inside another class. So it is itself a class but first it will act as a member of that class. As we know that we cannot make a class as static but inner class can be made as static… Read More
  • Constructor in Java Constructor is the member of the class and it has name same as class name. Constructor is the one of the important concept in java which is responsible to initialize the instance member of the class. Its functionality is lik… Read More
  • ' this ' keyword in Java this is one of the most important keyword in java which refers to the current class object. Like we are creating an object for our class similarly internally a object is there called as this which points to the current class… Read More

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

202004

Online Members

Live Traffic