Java program for ++ Increment Operator
Introduction
In this post, we will discuss the Increment Operator in java language. We will look at some simple java program using the operator. We will have a look at the postfix-increment operation and prefix-increment operation in the program.
Syntax
++ Increment operator: increments a value by 1
Postfix increment
b = a++;
Prefix increment
b = ++a;
Java Program
// IncrementOperatorDemo.java program
public class IncrementOperatorDemo {
public static void main(String[] args) {
int a = 1;
a++;
//print the variable
System.out.println("a after increment should print 2= " + a);
int b;
b= a++;// a is assigned to b and then incremented
//print the variable
System.out.println("b after post increment should print 2= " + b);
// a value is 3 now
b= ++a;// a value is incremented and assigned to b
//print the variable
System.out.println("b after pre increment should print 4= " + b);
}
}
In the program, we have initialized two variables a & b and then increment them using the operator. We print the post incremented ++ variables a & b.
Program Output
a after increment should print 2= 2
b after post increment should print 2= 2
b after pre increment should print 4= 4
Screenshot
