Java Incremental Operator
Java Incremental Operator
In Java, the incremental operator( ++ ) is used to increase the value of a variable by 1. Java provides two types of incremental operators:
- Post-Increment: The value is used first, then it is incremented. For example: var++
- Pre-Increment: The value is incremented first, then it is used. For example: ++var
Example
Let’s understand the operator with an example. The program asks the user for an integer. Using the incremental operator to print the next integer.
Java Code
import java.util.Scanner;
public class JavaIncrementOperatorExample {
public static void main(String[] args) {
int number; // declare variable
/* Scanner allows to read a number from standard input */
Scanner sc = new Scanner(System.in);
System.out.println("Enter an Integer? "); //ask user for an integer
number = sc.nextInt(); // store user input
/* Print the number */
System.out.println("The integer you entered is " + number);
/* Print the next number using the increment operator
prefix increments the variable and then prints */
System.out.println("The next integer is " + ++number );
sc.close();
} // end main
}
Screenshot
Java Tutorials
Java Tutorial on this website:
For more information on Java, visit the official website :