JAVA: Increment, Decrement operator

supriya2371997

Supriya Kolhe

Posted on December 9, 2020

JAVA: Increment, Decrement operator

Please go through Java: theory of Operators, Precedence & Associativity
for other operators and basics of below-mentioned operators

Q1: Increment operator?
Q2: Decrement operator?
Q3: Facts?

Increment operator:-
-: used to increment a value by 1.
-: 2 varieties are available that are pre-increment and post-increment.

  1. Pre-Increment: value is first used for calculating the result and then incremented
    For example X=42
    y=++X
    step1: x=x+1 (x=42+1=43)
    step2: y=x (y=43)

  2. Post-Increment: value is incremented and then calculated the result
    For example X=42
    y=X++
    step1: y=x (y=42)
    step2: x=x+1 (x=42+1=43)

Decrement operator:-
-: used to decrement a value by 1.
-: 2 varieties available that are pre-decrement and post-decrement

  1. Pre-Decrement: value is first used for calculating the result and then decremented
    For example X=42
    y=--X
    step1: x=x-1 (x=42-1=41)
    step2: y=x (y=41)

  2. Post-Decrement: value is decremented and then calculated the result
    For example X=42
    y=X--
    step1: y=x (y=42)
    step2: x=x-1 (x=42-1=41)

Alt Text

Facts:-
F1. Nesting like ++(++a) is not allowed
error: unexpected type

F2. Can be only used with variables.
program.java:9: error: unexpected type
a = 34++;
^
required: variable

F3. Can't be used on final variables
error: cannot assign a value to final variable a

F4. Can be only applied only on all primitive data types
error: bad operand type boolean for unary operator '++'

Please comment if you have any feedback or suggestions

💖 💪 🙅 🚩
supriya2371997
Supriya Kolhe

Posted on December 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related