Search Logic Blocks

Saturday, January 4, 2020

Java: An example for unary post / pre decrement (--) operators

Problem : Write a program to show an example for unary post / pre decrement operators.

Solution : 

Class Decrement (Decrement.java)
public class Decrement {
public static void main(String args[]) {
int y = 90;

System.out.println("Decrement Operator");

// Original Value
System.out.println("y = " + y);

// Post decrement operator
System.out.println("y-- = " + y--);

// After post decrement
System.out.println("y = " + y);

// Pre decrement operator
System.out.println("--y = " + --y);

// After pre decrement
System.out.println("y = " + y);
}
}
Here is the output: -

Decrement Operator
y   = 90
y-- = 90
y   = 89
--y = 88
y   = 88

Code can be found at Github Link.

No comments: