How to Use Increment and Decrement Operator in Java


In this example, we will learn how to use increment operator (++) and decrement operator(–) in Java.

Source Code

package com.beginner.examples;

public class IncExample {

    public static void main(String[] args){
    	 int a = 5;
		 int b = 5;
		 // a++
		 int m = a++;
		 int n = b++;
		 
		 System.out.println("m = " + m);
		 System.out.println("n = " + n);
		 // ++a
		 int j = ++a;
		 int k = ++b;
		 System.out.println("j = " + j);
		 System.out.println("k = " + k);
    }
}

Output:

m = 5
n = 5
j = 7
k = 7
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments