GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Java Increment and Decrement Operator

  • Java Programming Basics
  • Java Tutorial
  • Java Environment Setup
  • Java Separators
  • Java Data Types
  • Java Variables
  • Java Variable Scope
  • Java Type Casting
  • Java Operators
  • Java Increment Decrement
  • Java Left Shift
  • Java Right Shift
  • Java Bitwise Operators
  • Java Ternary Operator
  • Java Control Statements
  • Java if-else statement
  • Java for Loop
  • Java while Loop
  • Java do-while Loop
  • Java switch Statement
  • Java break Statement
  • Java continue Statement
  • Java Popular Topics
  • Java Arrays
  • Java Multidimensional Array
  • Java Strings
  • Java Methods
  • Java Date and Time
  • Java Exception Handling
  • Java File Handling
  • Java OOP
  • Java Classes and Objects
  • Java Constructors
  • Java Constructor Overloading
  • Java Object as Parameter
  • Java Returning Objects
  • Java Encapsulation
  • Java Abstraction
  • Java Inheritance
  • Java Polymorphism
  • Java Packages
  • Java Import Statement
  • Java Multithreading
  • Java Suspend Resume Stop Thread
  • Java Programming Examples
  • Java Programming Examples

Java Increment and Decrement Operator

The operator (++) and the operator (--) are Java's increment and decrement operators. The increment (++) and decrement operator (--) are simply used to increase and decrease the value by one.

The increment operator adds one to its operand, while the decrement operator subtracts one from its operand. Consider the following statement:

x = x + 1;

can be rewritten in this manner by using the increment operator, ++.

x++;

Similarly, the following statement:

x = x - 1;

can also be rewritten in this manner by using the decrement operator, --.

x--;

These operators are special because they can appear both in postfix and prefix form. In postfix form, they come after the operand. In prefix form, they come before the operand.

Prefix increment operator in Java

The prefix increment operator, of course, increases the value by one, but it does so before it is used. In Java, the prefix increment operator has the following general form:

++variable;

For example:

Java Code
public class PrefixIncrementExample {
    public static void main(String[] args) {
        int x = 5;
        int y = ++x;
        
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
Output
x = 6
y = 6

Postfix increment operator in Java

The postfix increment operator, of course, increases the value by one, but it does so after it is used. In Java, the postfix increment operator has the following general form:

variable++;

For example:

Java Code
public class PrefixIncrementExample {
    public static void main(String[] args) {
        int x = 5;
        int y = x++;
        
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
Output
x = 6
y = 5

Prefix decrement operator in Java

The prefix decrement operator, of course, decreases the value by one, but it does so before it is used. In Java, the prefix decrement operator has the following general form:

--variable;

For example:

Java Code
public class PrefixIncrementExample {
    public static void main(String[] args) {
        int x = 5;
        int y = --x;
        
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
Output
x = 4
y = 4

Postfix decrement operator in Java

The postfix decrement operator, of course, decreases the value by one, but it does so after it is used. In Java, the postfix decrement operator has the following general form:

variable--;

For example:

Java Code
public class PrefixIncrementExample {
    public static void main(String[] args) {
        int x = 5;
        int y = x--;
        
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
Output
x = 4
y = 5

Java Increment and Decrement Operators Example

This Java program demonstrates the increment operator.

public class JavaProgram {
   public static void main(String args[])  {
      int a = 1, b = 2, c = ++b, d = a++;
      c++;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println("c = " + c);
      System.out.println("d = " + d);
   }
}

When the above Java program is compiled and executed, it will produce the following output:

java increment and decrement operators

Now, this Java program demonstrates the decrement operator:

public class JavaProgram {
   public static void main(String args[])  {
      int a = 1, b = 2, c = --b, d = a--;
      c--;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println("c = " + c);
      System.out.println("d = " + d);
   }
}

When the above Java program is compiled and executed, it will produce the following output:

java increment and decrement operators example

Java Online Test


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.