Advertisement



< Prev
Next >



Decrement Operator










Pre-decrement operation


This operation is named pre-decrement because the decrement operator(--) comes before the operand.
Sequence of operation in pre-decrement operation is -
class A
{
public static void main(String... ar)
{
char ch= 'g';
byte b=10;
short sh= 200;
int i=50;
long l=999999;
float f=10.5f;
double d= 20.5;



System.out.println("Original character " + ch);
System.out.println("Decremented character " + --ch);

System.out.println("Original byte " + b);
System.out.println("Decremented byte " + --b);

System.out.println("Original short " + sh);
System.out.println("Decremented short " + --sh);

System.out.println("Original int " + i);
System.out.println("Decremented int " + --i);

System.out.println("Original long " + l);
System.out.println("Decremented long " + --l);

System.out.println("Original float " + f);
System.out.println("Decremented float  " + --f);

System.out.println("Original double " + d);
System.out.println("Decremented double " + --d);

}
}


Output is


Original character g
Decremented character f
Original byte 10
Decremented byte 9
Original short 200
Decremented short 199
Original int 50
Decremented int 49
Original int 99999
Decremented int 99998
Original float 10.5
Decremented float  9.5
Original double 20.5
Decremented double 19.5





Post-Decrement operator


class A
{
public static void main(String... ar)
{
char ch= 'g';
byte b=10;
short sh= 200;
int i=50;
long l=999999;
float f=10.5f;
double d= 20.5;



System.out.println("Original character " + ch);
System.out.println("Decremented character " + ch--);

System.out.println("Original byte " + b);
System.out.println("Decremented byte " + b--);

System.out.println("Original short " + sh);
System.out.println("Decremented short " + sh--);

System.out.println("Original int " + i);
System.out.println("Decremented int " + i--);

System.out.println("Original long " + l);
System.out.println("Decremented long " + l--);

System.out.println("Original float " + f);
System.out.println("Decremented float  " + f--);

System.out.println("Original double " + d);
System.out.println("Decremented double " + d--);

}
}
Output -
Original character g
Decremented character g
Original byte 10
Decremented byte 10
Original short 200
Decremented short 200
Original int 50
Decremented int 50
Original int 99999
Decremented int 99999
Original float 10.5
Decremented float  10.5
Original double 20.5
Decremented double 20.5


Program Analysis





Advertisement




Displaying decremented value after a post-decrement operation


class A
{
public static void main(String... ar)
{
char ch= 'h';
byte b=10;
short sh= 100;
int i=10;
long l=99999;
float f=20.5f;
double d= 15.5;


System.out.println("Original character " + ch);
ch--;
System.out.println("Decremented character " + ch);

System.out.println("Original byte " + b);
b--;
System.out.println("Decremented byte " + b);

System.out.println("Original short " + sh);
sh--;
System.out.println("Decremented short " + sh);

System.out.println("Original int " + i);
i--;
System.out.println("Decremented int " + i);

System.out.println("Original long " + l);
l--;
System.out.println("Decremented long " + l);

System.out.println("Original float " + f);
f--;
System.out.println("Decremented float  " + f);

System.out.println("Original double " + d);
d--;
System.out.println("Decremented double " + d);

}
}


Output-


Original character h
Decremented character g
Original byte 10
Decremented byte 9
Original short 100
Decremented short 99
Original int 10
Decremented int 9
Original float 20.5
Decremented float  19.5
Original double 15.5
Decremented double 14.5


Program Analysis







Post and pre-decrement operators in arithmatic expressions.


class A
{
public static void main(String... ar)
{
int a =10, b=10, c=10, d=10;

int result = a-- - 10; //first value of a(10) is read and subctracted with 10 and then it is decremented to 9
System.out.println("Result1 = " + result);
System.out.println("a = "+ a);

result = --b - 10; //first the value of a(9) is decremented to 8 and then it is subtracted by 10.
System.out.println("Result2 = " + result);
System.out.println("b = "+ b); 

result = --c + c--;
System.out.println("Result3 = " + result);
System.out.println("c = "+ c);

result = d-- + 10 + --d;
System.out.println("Result4 = " + result);
System.out.println("d = "+ d);
}
}


Output-


Result1 = 0
a = 9
Result2 = -1
b = 9
Result3 = 18
c = 8
Result4 = 28
d = 8





Explaining the sequence of operations in our last code.



a-- - 10
  1. Due to post-decrement operator with a, its value 10 is first read to be subtracted by 10.
  2. Next, the original value of a is decremented to 9.
  3. Finally, result of 10-10 = 0, is passed to the result.



--b - 10
  1. Due to pre-decrement operator with b, its value 10 is first decremented to 9
  2. Next, this decremented value of b, 9 is subtracted by 10
  3. Finally, result of 9-10 = -1 , is passed ot the result.




--c + c--
  1. Due to pre-decrement operator with c, first its value 10 is decremented to 9
  2. In the next expression, due to post-decrement operator with c, first the current value of c i.e. 9 is read and added to 9 from the previous expression and then c is decremented to 8
  3. Finally, the result of 9+9 = 18, is passed to the result.




d-- + 10 + --d
  1. Due to post-decrement operator with d, first its value is read to be added to 10, resulting 20 and then d is decremented to 9
  2. In the next expression, due to pre-decrement operator with d, first the current value of d 9 is first decremented to 8 and then it is added to 20 from the previous expression.
  3. Finally, the result of 20+8 = 28, is passed to the result.




Please share this article -




< Prev
Next >
< Increment Operator
Compound Assignment Operators>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement