As the name indicates unary Operators act on only one operand.There are 3 kinds of unary operators

  1. uniary minus operator(-)
  2. Increment operator(++)
  3. Decrement operator(–)

Unary minus operator

This operator is used to negative a given value.

Example:

public class Test {
	public static void main(String[] args) {
		int x=5;
		System.out.println(-x);
		System.out.println(-(-x));
	}
}
/*
Output:
-5
 5
*/

Increment operator(++)

  1. Writing ++ before a variable is called “Pre incrementation
  2. Writing ++ after a variable is called “Post incrementation

In pre incrementation ,Incrementation is done first and any other operation is done next.
In post Incrementation ,all other operations are done first and incrementation is one only at the end.

public class Test {
	public static void main(String[] args) {
		int x=1;
		System.out.println(x); 
		System.out.println(++x);
		System.out.println(x); 
	}
}
/*
Output:
 1
 2
 2
*/

Example:

public class Test {
	public static void main(String[] args) {
		int x=1;
		System.out.println(x);
		System.out.println(x++);
		System.out.println(x);
	}
}
/*
Output:
 1
 1
 2
*/

Example:


public class Test {
	public static void main(String[] args) {
		int a=1,b=2;
		a=++b;
		System.out.println(a);//3
		System.out.println(b);//3
	}
}
/*
Output:
 3
 3
*/

Example:

public class Test {
	public static void main(String[] args) {
		int a=1,b=2;
		a=b++;
		System.out.println(a);
		System.out.println(b);
	}
}
/*
Output:
 2
 3
*/

Decrement Operator(- -)

This operator is used to decrement the value of a variable by 1.

  1. Writing – -before variable is called pre-decrementation.
  2. Writing – -after variable is called pre-decrementation.

Example:

public class Test {
	public static void main(String[] args) {
		 int x=5;
		 System.out.println(--x);
		 System.out.println(x--);
	}
}

/*
Output:
 4
 4
*/
Expression Initial value of x Final value of y Final value of x
y=++x 4 5 5
y=x++ 4 4 5
y=- -x 4 3 3
y=x- – 4 4 3
  1. Increment/Decrement operators applicable only for variables but not for constant values
  2. Example:

    
    public class Test {
       public static void main(String[] args) {
    	int x=4;
    	int y=++x;
    	System.out.println(x);
    	System.out.println(y);
       }
    }
    /*
    Output:
     5
     5
    */
    

    Example:

    
    public class Test {
      public static void main(String[] args) {
    	int y=4++;//C.E
    	System.out.println(y);
      }
    }
    /*
    Output:
    Compile time error: Unexpected type
    required:variable
    found :value
    */
    
  3. We can’t nest increment and decrement operators.
    Example:

    
    public class Test {
      public static void main(String[] args) {
    	int x=4;
            int y=++(++x);//C.E
            System.out.println(y);
      }
    }
    /*
    Compile time error:Unexpected type
    required:variable
    found :value
    */
    
  4. we cannot increment/decrement final variables.
    Example:

    public class Test {
      public static void main(String[] args) {
    	final int x=4;
            x++;//C.E
            System.out.println(x);
     }
    }
    /*
    Compile time Error:Cannot assign a value to final variable x.
    */
    
  5. we can apply increment/decrement operators even for floating point data types also.
    Example:

    public class Test {
      public static void main(String[] args) {
    	float d=10.5f;
    	d++;
    	System.out.println(d);
      }
    }
    /*
    Output:
    11.5
    */
    

    Output:11.5

Difference between b++ and b=b+1?

When ever we are applying arithmetic operation b/n two operands a and b the result type is:max(int,type of a,type of b);

Example:

public class Test {
	public static void main(String[] args) {
		  byte a=10;
		  byte b=20;
		  byte c=a+b; //max(int,byte,byte)=int
		  System.out.println(c);
	}
}
/*
Compile time error:Possible loss of precision
found:int
required:byte
*/

In the case of increment/decrement operators, internal type casting is performed by the compiler hence ,b++=>(type of b)(b+1)
Example:


public class Test {
public static void main(String[] args) {
  byte b=10;
  b++;
  System.out.println(b);
 }
}
/*
Output:
11
*/

Example:


public class Test {
	public static void main(String[] args) {
		byte b=10;
		b=b+1;//C.E
		System.out.println(b);
	}
}

/*
Compile time error:Possible Loss of precision
found:int
required:byte
*/