Want to keep learning?

This content is taken from the University of Reading's online course, Begin Programming: Build Your First Mobile Game. Join the course to learn more.

Unary operator

The word ‘unary’ refers to a single unit. Unary operators are so-called because they perform operations on a single operand.

For the purposes of this course, we will only discuss one useful unary operator: ! This operator inverts the value of a Boolean. So, if a Boolean variable has a value of true, using this operator will make the result false.

For example:

boolean myValue = true;   
boolean myResult = !myValue;   

/*after execution of the line
 boolean myResult = !myValue 
 the variable myResult will have a value of false*/

Share this article:

This article is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading