Increment and decrement operators
Two operators you will find yourself using frequently in Java are the increment and decrement operators, ++ and - -. These have two forms: postfix and prefix. The ++ operator increments the variable it is associated with by 1 and the -- operator decrements the value of the variable it is associated with by 1. Suppose we have:
int myInt = 0;
myInt++; // postfix increment
++myInt; // prefix increment
After the first increment myInt has the value 1 and after the second it has the value 2. In this context, there is no difference between the prefix and postfix operators.
However, the increment and decrement operations return values that can be used in any context where their type is valid, and this is where these operators differ. The postfix variant (in which the operator appears after the argument) returns the old value of the variable to which it is applied whereas the prefix form returns the new value.
For example, consider:
int myInt = 10;
int x = myInt++; // postfix form
The second statement increments the value of the variable myInt and places this value back into myInt. It then returns the old value of myInt and this is stored in x. After this code, myInt would have the value 11 and x would have the value 10.
However, it would be different if we had written:
int myInt = 10;
int x = ++myInt; // prefix form
After this code, both x and myInt would have the value 11, since the prefix increment operator returns the value of myInt after it is incremented.
The action of the -- operators is the same, except that subtraction rather than addition occurs.
The properties of the ++ and -- operators are summarized in Table.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Increment and decrement operators in Java homework and assignments? Live Increment and decrement operators in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Increment and decrement operators in Java homework help, java assignment help and Increment and decrement operators in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours