in Technology by
What is Decrement Operator in JAVA?

1 Answer

0 votes
by

It is used to decrement the value of an operand by 1. The operator is represented by a pair of minus operators (--). The operator can be applied before or after an operand.

Pre-decrement Operator: If a decrement operator is written before (prefix) the operand is known as pre-decrement. In such a case, the value is first decremented by 1 and then used for the computing purpose.

Syntax:

  1. --operand  

Example: Suppose x=9, then the value of --x will be 8.

Post-decrement Operator: If a decrement operator is written after (postfix) the operand is known as post-decrement. In such a case, the value is first used and then decrements by 1.

Syntax:

  1. operand--  

Example: Suppose x=11, then the value of x-- will be 10.

Let's use the pre-decrement and post-decrement operators in a Java program.

PrePostDecrementExample.java

  1. public class PrePostDecrementExample   
  2. {  
  3. public static void main(String args[])  
  4. {  
  5. int i = 10;  
  6. i--;  
  7. //prints 19  
  8. System.out.println(i);  
  9. --i;                 
  10. //prints 18  
  11. System.out.println(i);  
  12. //prints 17  
  13. System.out.println(--i);  
  14. //prints 17  
  15. System.out.println(i--);  
  16. //prints 16  
  17. System.out.println(i);  
  18. }  
  19. }  

Output:

19
18
17
17
16

Related questions

0 votes
    Decrement operator, −−, decreases the value of variable by what number? (a) 1 (b) 2 (c) 3 (d ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    What is Indirection and Increment/Decrement operators with a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    What is the process to create increment and decrement statement in C?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    Consider following piece of code void foo( bool forwad ) { vector::iterator it, end_it; int dir; ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Consider following piece of code void foo( bool forwad ) { vector::iterator it, end_it; int dir; ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I try to learn and implement a simple genetic algorithm library for my project. At this time, evolution, ... Gaussian distribution?.) Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Which operator is used by Java run time implementations to free the memory of an object when it is no ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which of the following is the truncation division operator in Python? a) | b) // c) / d) %...
asked Dec 31, 2022 in Technology by JackTerrance
0 votes
    In MongoDB, _________ is the operator limits the contents of an array field from the query. (1)$Match (2)$elemMatch (3)$slice...
asked May 23, 2021 in Technology by JackTerrance
0 votes
    What is the address of operator ( &)?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is Indirection or Dereference Operator ( * )?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Which operator can be used to access union elements if union variable is a pointer variable?...
asked Jan 18, 2021 in Technology by JackTerrance
0 votes
    How does bitwise operator XOR works?...
asked Jan 18, 2021 in Technology by JackTerrance
0 votes
    Which operator can be used to determine the size of a data type or variable?...
asked Jan 18, 2021 in Technology by JackTerrance
...