Java Increment Decrement Operators(++) | (- -)

Dhanuka lakshan
2 min readApr 11, 2020

Increment Operator (++)

Increment has two operation.They are Pre- increment and post-increment.I did like to explain first pre- increment.In this case increased value and Assign to variable.Check it out below the example to make sure.

class StarPtn
{
public static void main(String args[])
{
int age = 13;
++age;//-----> age = age + 1
System.out.println("My Age is : " + age);

}
}
Output------->
C:\Users\Dhanuka Lakshan\Desktop\java>java StarPtn
My Age is : 14

I did like to explain second post-increment. In this case increased value and not Assign to variable. But if you print variable next line show up together. Check it out below the example to make sure.

class StarPtn
{
public static void main(String args[])
{
int age = 13;
System.out.println("My age is : "+age++);//13
System.out.println("My New Age is : "+age);//14


}
}
Output------------>
C:\Users\Dhanuka Lakshan\Desktop\java>java StarPtn
My age is : 13
My New Age is : 14

Decrement Operator (- -)

I did like to explain first pre- Decrement.In this case reduced by one value and Assign to variable.Check it out below the example to make sure.

class StarPtn{

public static void main(String args[])

{
int age = 13;
--age;//-----> age = age - 1
System.out.println("My Age is : " + age);
}}
Output-------->
C:\Users\Dhanuka Lakshan\Desktop\java>java StarPtn
My Age is : 12

I did like to explain second post-Decrement. In this case reduced by one value and not Assign to variable. But if you print variable next line show up together. Check it out below the example to make sure.

class StarPtn{

public static void main(String args[])

{

int age = 13;

System.out.println("My Age is : " + age--);
System.out.println("My New Age is : " + age);}}
Output------>
C:\Users\Dhanuka Lakshan\Desktop\java>java StarPtn
My Age is : 13
My New Age is : 12

--

--

Dhanuka lakshan

I’m a Software engineering student who is currently studying @Institute_of_Java And Software Engineering(IJSE), I like coding and learn about new tech.