Pre-incrementation and post-incrementation - Java Language Basics

Java examples for Language Basics:Operator

Introduction

Often we need to add or subtract one to the value of a variable, say x.

This can be done as follows:

Demo Code


public class Main {
  public static void main(String arg[]) {
    int x = 1;//  w  w  w. ja  v  a  2  s  .  co  m
    System.out.println(x);
    x=x+1; 
    System.out.println(x);
    x+=1; // compact form 
    System.out.println(x);
    x=x-1; 
    System.out.println(x);
    x-=1; // compact form 
    System.out.println(x);

  }
}

Result


Related Tutorials