Skip to main content

Java increment & decrement operator

 

Increment and Decrement Operator in Java :

  1. It is one of the variation of “Arithmetic Operator“.
  2. Increment and Decrement Operators are Unary Operators.
  3. Unary Operator Operates on One Operand.
  4. Increment Operator is Used to Increment Value Stored inside Variable on which it is operating.
  5. Decrement Operator is used to decrement value of Variable by 1 (default).

Types of Increment and Decrement Operator :

  1. Pre Increment / Pre Decrement Operator
  2. Post Increment / Post Decrement Operator

Syntax :

++ Increment operator : increments a value by 1
-- Decrement operator : decrements a value by 1

Pre-Increment Operator :

  1. “++” is written before Variable name.
  2. Value is Incremented First and then incremented value is used in expression.
  3. “++” cannot be used over “Constant” of “final Variable“.

Live Example 1 : Post Incrementing Variable

[468×60]

class PostIncrement {
  public static void main(String args[]) {
    int num1 = 1;
    int num2 = 1;

    num1++;
    num2++;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
  }
}

Output :

num1 = 2
num2 = 2
  • Post Increment : Increment Value of Variable After Assigning
  • num1++ will increment value inside “num1” variable after assigning old value to itself.
  • New Value of num1 = 2.

Live Example 2 : Pre Incrementing Variable

class PreIncrement {
  public static void main(String args[]) {
    int num1 = 1;
    int num2 = 1;

    --num1;
    --num2;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
  }
}

Output :

num1 = 0
num2 = 0
  • Pre Increment : First Increment Value and then Assign Value.
  • ++num1 will increment value inside “num1” variable. New value is assigned to itself.
  • New Value of num1 = 0.

Live Example 3 : Post Incrementing Variable

Increment and Decrement in Java

class PreIncrement {
  public static void main(String args[]) {
    int num1;
    int num2;
    int num3;

    num1 = 100;
    num2 = ++num1;
    num3 = num2++ + ++num1;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
    System.out.println("num3 = " + num3);
  }
}

Explanation :

  • Initially num1 = 100.

Consider this Expression –

num2 = ++num1;
  • Value is Assigned to Variable Present at Left hand Side, just after incrementing  “num1” .
Step 1 : Increment Value of num1.
Step 2 : New Value of num1 = 101
Step 3 : Assign Value of num1 to num2
Step 4 : New Value of num2 = 101

Consider this Expression –

num3 = num2++ + ++num1;
  • During Expression evaluation only pre incremented value is used. (post increment operator is used just after completion of expression evaluation)
Step 0 : Initial Values -
         num1 = 101
         num2 = 101
Step 1 : Increment Value of num1 as it is (pre-increment)
Step 2 : Keep Value of num2 as it is (post-increment)
Step 3 : At this stage -
         num1 = 102
         num2 = 101
Step 4 : Add num1 & num2
         num3 = Old(num2) + new(num1)
         num3 = 101 + 102
         num3 = 203
Step 5 : Expression Completed
         num3 = 203
Step 6 : Increment Value of num2 (pending post-increment)
         num2 = 102
Step 7 : Final Values
         num1 = 102
         num2 = 102
         num3 = 203

[468×60]

Live Example 4 : Incrementing Variable inside Println Method

class PrePostDemo {
    public static void main(String[] args){
        int i = 3;
        i++;

        // prints 4
        System.out.println(i);
        ++i;

        // prints 5
        System.out.println(i);

        // prints 6
        System.out.println(++i);

        // prints 6
        System.out.println(i++);

        // prints 7
        System.out.println(i);
    }
}
  1. If Inside println we have post increment then firstly Old value gets printed on screen and after printing variable gets new value.
  2. In case of pre increment Variable gets new value by executing increment operation and new value will be printed on the screen.

Live Example 5 : Yet another Example of Increment Operator

class IncDec {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c;
    int d;
    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);
  }
}

Output :

a = 2
b = 3
c = 4
d = 1

Live Example 6 : Incrementing Final Value

class IncrementFinal {

    public static void main(String[] args){

        final int constVal = 10;
        System.out.println(constVal);
    }
}

Output :

Compile Time Exception will be thrown
  • We cannot increment or decrement Constant Value in Java.
  • Constant Value in Java are nothing but final values.

Comments

Popular posts from this blog

Privacy Policy

Privacy Policy Privacy Policy This privacy policy applies to the Rhino VPN app (hereby referred to as "Application") for mobile devices that was created by Codewithyaji (hereby referred to as "Service Provider") as a Free service. This service is intended for use "AS IS". What information does the Application obtain and how is it used? The Application does not obtain any information when you download and use it. Registration is not required to use the Application. Does the Application collect precise real time location information of the device? This Application does not collect precise information about the location of your mobile device. Do third parties see and/or have access to information obtained by the Application? Since the Application does not collect any information, no data is shared with third parties. What are my opt-out rights? You can stop all collection of information by the Application easily by uninstalling it. Yo...

Privacy Policy

Privacy Policy Privacy Policy Yajindra Gautam built the Gstatus app as an Ad Supported app. This SERVICE is provided by Yajindra Gautam at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used ...

Privacy Policy for yajindra

Privacy Policy for yajindra At instagram-analytic, accessible from https://instagram-analytic.yajindragautam.com.np, one of our main priorities is the privacy of our visitors. This Privacy Policy document contains types of information that is collected and recorded by instagram-analytic and how we use it. If you have additional questions or require more information about our Privacy Policy, do not hesitate to contact us. This Privacy Policy applies only to our online activities and is valid for visitors to our website with regards to the information that they shared and/or collect in instagram-analytic. This policy is not applicable to any information collected offline or via channels other than this website. Consent By using our website, you hereby consent to our Privacy Policy and agree to its terms. Information we collect The personal information that you are asked to provide, and the reasons why you are asked to provide it, will be made clear to you at the point we as...