DEV Community

Supriya Kolhe
Supriya Kolhe

Posted on • Edited on

1 1

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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay