❮ Previous Next ❯

Java Increment and Decrement Operators(Unary Operator)

The java operator taking one operand is called Increment and Decrement Operators(Unary Operator) .

The most basic two unary operators such as increment operator(++) and decrement operator(--).

Again increment operator is divided into two categories such as pre-increment and post-increment.

Similarly decrement operator is also devided into pre-decrement and post-decrement.

In case of pre-increment/ decrement first the value of variable will increment / decrement a value by 1 , then it will display.

where as in case of post-increment / decrement, first the value of variable will display then it will increment / decrement.


Let's see some unary operators :

  +   // Unary plus operator; indicates positive value (numbers are positive without this, however)
  -   //Unary minus operator; negates an expression
  ++   //Increment operator; increments a value by 1
  --   //Decrement operator; decrements a value by 1
  !   //Logical complement operator; inverts the value of a Boolean

let's see some following small examples :

  class Increment
  {
  public static void main(String args[])
  {
  int x=5,y;
  y=x++ + x++ + ++x + x++ + ++x;
  System.out.println("y="+y);
  }
  }
  Output
  y=37
  class TestUnary
  {
  public static void main(String args[])
  {
  int x=8,y;
  y=++x + ++x - x++ + ++x - --x;
  System.out.println("y="+y);
  }
  }
Output
y=10
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved