Home > Java > javaTutorial > body text

Working with Unary Operators

Barbara Streisand
Release: 2024-11-11 09:20:02
Original
421 people have browsed it

By definition, a unary operator is one that requires exactly one operand, or variable, to
function.They often perform simple tasks, such as increasing a
numeric variable by one, or negating a boolean value.

Working with Unary Operators

Logical Complement and Negation Operators
The logical complement operator, !, flips the value of a boolean expression. For example,
if the value is true, it will be converted to false, and vice versa. To illustrate this, compare
the outputs of the following statements:

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

Likewise, the negation operator, -, reverses the sign of a numeric expression, as shown
in these statements:

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
Copy after login
Copy after login

Based on the description, it might be obvious that some operators require the variable
or expression they’re acting upon to be of a specific type. For example, you cannot apply
a negation operator, -, to a boolean expression, nor can you apply a logical complement
operator, !, to a numeric expression. Be wary of questions on the exam that try to do this,
as they’ll cause the code to fail to compile. For example, none of the following lines of code
will compile:

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

The first statement will not compile due the fact that in Java you cannot perform a
logical inversion of a numeric value. The second statement does not compile because you
cannot numerically negate a boolean value; you need to use the logical inverse operator.
Finally, the last statement does not compile because you cannot take the logical complement of a numeric value, nor can you assign an integer to a boolean variable.

in Java 1 and true are not related in
any way, just as 0 and false are not related.

Increment and Decrement Operators
Increment and decrement operators, and --, respectively, can be applied to numeric
operands and have the higher order or precedence, as compared to binary operators. In
other words, they often get applied first to an expression.
Increment and decrement operators require special care because the order they are
applied to their associated operand can make a difference in how an expression is processed. If the operator is placed before the operand, referred to as the pre-increment operator and the pre-decrement operator, then the operator is applied first and the value return
is the new value of the expression. Alternatively, if the operator is placed after the operand,
referred to as the post-increment operator and the post-decrement operator, then the original value of the expression is returned, with operator applied after the value is returned.
Here is the example illustration:

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

The first pre-increment operator updates the value for counter and outputs the new
value of 1. The next post-decrement operator also updates the value of counter but outputs
the value before the decrement occurs.
One common practice in a certification exam, albeit less common in the real world, is to
apply multiple increment or decrement operators to a single variable on the same line:

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
Copy after login
Copy after login

This one is more complicated than the previous example because x is modified three
times on the same line. Each time it is modified, as the expression moves from left to right,
the value of x changes, with different values being assigned to the variable. As you’ll recall
from our discussion on operator precedence, order of operation plays an important part in
evaluating this example.
So how do you read this code? First, the x is incremented and returned to the expression,
which is multiplied by 5. We can simplify this:

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

Next, x is decremented, but the original value of 4 is used in the expression, leading to
this:

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
Copy after login

The final assignment of x reduces the value to 2, and since this is a pre-increment operator, that value is returned to the expression:

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

Finally, we evaluate the multiple and division from left-to-right, and finish with the addition. The result is then printed:

int y = 4 * 5 / x-- + --x; // x assigned value of 4
Copy after login

To conclude, unary operators simplify and streamline code by enabling quick, single-operand operations like incrementing, negating, or inverting values. They are essential for efficient logic, compact expressions, and concise control over data, enhancing readability and functionality in programming.

The above is the detailed content of Working with Unary Operators. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template