Unary operators in Java

Tomorrow OTUS will soon start a new professional preparation for Oracle Java Programmer (OCAJP) certification… We invite you to watch the recording of a free Demo lesson Java Data Types: Identifiers and Primitives and we continue to publish a series of articles by Vladislav Rodin – the head of the development group, teacher at MIPT and foxminded.


Introduction

The OCA (Oracle Certified Associate Java SE8 Programmer) certification offers a series of quirky assignments that test a deep understanding of the Java programming language. The whole block is devoted to expressions, loops and operators. We will talk about the latter today.

Operator priority

The operator accepts arguments as input and returns some values. There are unary, binary, and ternary operators. For instance, ! false – unary, a + b – binary, and? : – is the only operator that takes three arguments.

The first thing to remember is the precedence of the operators:

  1. Post-unary operators: exception ++ and exception–
  2. Pre-unary operators: ++ exception and –exception
  3. The rest of the unary operators: +, -,!
  4. Multiplication, division, taking the remainder: *, /,%
  5. Addition and Subtraction: +, –
  6. Bit shift operators: <<, >>, >>>
  7. Comparison operators: <,>, <=,> =, instanceof
  8. Equality-inequality operators: ==,! =
  9. Logical operators: &, |, ^
  10. Short-circuit logical operators: &&, ||
  11. Ternary operator: boolean expression? expression1: expres-
    sion2
  12. Assignment operators: =, + =, – =, * =, / =,% =, & =, ^ =,! =, << =, >> =, >>> =

Working with unary operators

We talked about binary operators last time. Today we will discuss unary operators, which follows from the title of the article. On the exam, they may offer quite complex expressions for the calculation of which you must remember the order of their execution.

Sign change and logical inversion operators

Boolean Inversion Operator ! applies only to variables of type boolean and turns the value from true to false and vice versa. For instance:

boolean x = false;
System.out.println(x); // false
x = !x;
System.out.println(x); // true

Sign change operator applies only to numbers and reverses the sign:

double x = 1.21;
System.out.println(x); // 1.21
x = -x;
System.out.println(x); // -1.21
x = -x;
System.out.println(x); // 1.21

Java, in contrast to the same C, clearly separates integer data types and boolean, and therefore the use of the sign change operator to boolean or the use of the logical inversion operator lead to compilation errors:

int x = !5; // DOES NOT COMPILE
boolean y = -true; // DOES NOT COMPILE
boolean z = !0; // DOES NOT COMPILE

Increment and decrement operators

Increment operators ++ and decrement apply to integer variables and have two variations: post-increment (decrement) and pre-increment (decrement), in the code i ++ (i–) and ++ i (–i) respectively. The difference between the variations is that ++ i increments the variable and returns the new value, while i ++ returns the old value and only then increments the variable. The difference can be seen more clearly in this piece of code:

int counter = 0;
System.out.println(counter); // Outputs 0
System.out.println(++counter); // Outputs 1
System.out.println(counter); // Outputs 1
System.out.println(counter--); // Outputs 1
System.out.println(counter); // Outputs 0

On the exam, various tricky tasks may be offered, where such operators are a wagon and a small cart. For example, the following task may be offered:
What will be displayed as a result of this program?

int x = 3;
int y = ++x * 5 / x-- + --x;
System.out.println("x is " + x);
System.out.println("y is " + y);

The problem is that x changes multiple times on the same line.

To solve such problems, you can parse the expression from left to right, substituting in the expression what the operators return, and separately fixing the value of the variable being changed. For example, like this:

int y = 4 * 5 / x-- + --x; // x assigned value of 4
int y = 4 * 5 / 4 + --x; // x assigned value of 3
int y = 4 * 5 / 4 + 2; // x assigned value of 2

As you can see, all of the above rules for applying operators apply. The result will be the following:

x is 2
y is 7

Read more:

  • Binary Operators in Java

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *