Home Java javaTutorial Working with Unary Operators

Working with Unary Operators

Nov 11, 2024 am 09:20 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1241
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles