Operators in Java(part2)

Damitha karunarathne
4 min readMay 6, 2020

Hi readers!This is my Fifth article and it is ,second part of the Operators in java.I think you have read first part of operators and got an idea about operators.With in this article I may go some deep with operators.

Increment /decrement operators

An increment or decrement operator provides a more convenient and compact way to increase or decrease the value of a variable by one.

  • ++i = Pre-increment = first increase then asign
  • i++ = Post-increment = first asign then increase

This applies to decrement too.

int x=5;
++x; //answer is 6
--x; //answer is 4

prefix and postfix

prefix and postfix, may be used with both the increment and decrement operators.while in postfix form, the operator appears after the operand.

Prefix

Increments the variable’s value and uses the new value in the expression.With prefix form, the operator appears before the operand.

int x = 34;
int y = ++x; // y is 35

Postfix

The variable’s value is first used in the expression and is then increased.with in postfix form, the operator appears after the operand.

int x = 34;
int y = x++; // y is 34

To understand more about this prefixes and postfixes let us see following examples.I hope it will help you to understand them well.

ex:1

int test=5;
System.out.println(test++); // output: 5 (postfix) //a
System.out.println(++test); // output: 7 (prefix) //b
System.out.println(test+1); // output: 8 (addition) //c

Here in ‘a’ answer comes as 5.As it is postfix. But when it goes to the ‘ b ‘ the value of the test is 6.In ‘b’ there is prefix.So first increase the value and the answer is 7.When it goes to ‘c’ the value of test is 7 and adding 1.Then the answer is 8.

ex:2

int x=5;
++x;
System.out.println(++x) // answer = 7

Here at first++x there is prefix.So first add one value.At that time answer is 6.Then again at second ++x adding one value.So the answer is 7.

ex:3


int x=42;
int y=++x;
System.out.println(x); // answer is 43
System.out.println(y); // answer is 43
}}

ex:4

x=42 ;
y=x++;
System.out.println(x); // answer is 43
System.out.println(y); // answer is 42

ex:5

int test = 5;
++test;
System.out.println(test); //output is 6 //a
test++; //b
System.out.println(test++); //output is 7, //c
System.out.println(test); //output now reveals itself as 8 //d

Here at first ++test there is prefix.So at ‘a’ answer is 6.Now the value is 6.At second test++ , there is postfix and now value is 6.At c test++ value comes as 8, but output is 7.At ‘d’ now the value is 8.

  • ++x is pre increment // result comes before the statement.
  • x++ is post increment // result comes after the statement.

There are some more examples in following.Follow them carefully and take an idea about more about prefixes and postfixes.

ex:6

int test = 5;
test++;
System.out.println((test+2)*2); // 16

ex:7

int test = 2;
int result= (test++ + ++test ); // output=6(2+4)

ex:8

int x = 34;  
System.out.println(x++); //34
System.out.println(x); //35
System.out.println(++x); //36
System.out.println(x--); //36
System.out.println(--x); //34

ex:9

c=5;                         // assign 5 to c      
System.out.println(c); //prints c
System.out.println(c++); // prints 5 then post increment System.out.println(c); //prints 6

ex:10


int a=7,b=2;
System.out.println(a+" "+b); // 7 2
a++; // 7
++b; // 3
System.out.println(a+" "+b); // 8 3

ex:11


int a=7,b=2;
System.out.println(a+" "+b); // 7 2
--a; // 6
b--; // 2
System.out.println(a+" "+b); // 6 1

ex:12

int x = 34;         
int y = x++; // y holds a value of x first, 34. then int x is added by 1, int x is now 35 //
System.out.println(x++); // x will print first, 35, then int x will be added by 1. now int x = 36 //
System.out.println(y);// y still holds original value of x, 34 System.out.println(x); // now we print the value of x, 36

ex:13

int x = 34; 
System.out.println(x++-x++); //output: -1 (34-35)

ex:14

int x = 34;
int a =++x;
int b =x++;
System.out.println(a); //35
System.out.println(b); //35
System.out.println(x); //36

ex:15

int z, x=0;   
z = ++x;
System.out.println(z+x) ; //Output: 2 (1+1)
System.out.println(z+""+x); // Output: 11

ex:16

int test = 5;   
++test;
System.out.println(test); // 6 (5+1)
System.out.println(test++); // 6
System.out.println(test--); // 7 (6+1)

ex:17

int x = 34;        
int y = x++;
int z = ++x;
System.out.println(z); // 36

ex:18

int x = 34;         
System.out.println(x--); // 34
System.out.println(x--); // 33
System.out.println(x++); // 32
System.out.println(x++); // 33
System.out.println(--x); // 33
System.out.println(x--); // 33
System.out.println(++x); // 33
System.out.println(x++); // 33
System.out.println(++x); // 35

ex19:

int a=7,b=2;
System.out.println(a+" "+b); // 7 2
a=b++;
System.out.println(a+" "+b); // 2 3

ex20:

int a=7,b=2;
System.out.println(a+" "+b); // 7 2
b=a--;
System.out.println(a+" "+b); // 6 7

ex21:

int a=7,b=2;
System.out.println(a+" "+b); // 7 2
a=a++;
System.out.println(a+" "+b); // 7 2

ex22:

int a=7,b=2;
System.out.println(a+" "+b); // 7 2
b=b++;
System.out.println(a+" "+b); // 7 2

ex23:

int a=7,b=2;
System.out.println(a++ + a++ + a++); // (7+8+9=24)

ex24:

int a=7,b=2;
System.out.println(a++ + b++ - --b); // (7+2-2=7)

ex25:

int a=7,b=2;
System.out.println(++a + ++a + ++a); //(8+9+10=27)

ex26:

int a=7,b=2;
System.out.println(a++ + b++ - --b); //(7+2-2=7)

These are some few examples that can get more idea about prefixes and postfixes. With in next article I hope to discuss about assignment operators and bit-wise operators. Thank you for reading.Keep in touch.

--

--