Unary Operator in Java W3Schools Guide Learn Syntax Usage with Examples

Robert Jul 13, 2026

The unary operator in Java, much like its counterparts in other programming languages, performs operations on a single operand. It's a crucial part of the language's syntax, enabling concise and expressive code. Let's delve into the world of unary operators in Java, using examples from W3Schools as our guide.

☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀

Unary operators in Java can be further categorized into two types: operational and casting. Let's explore each type, along with their syntax and usage.

the front cover of an operating manual with text that reads unary operators single operand power
the front cover of an operating manual with text that reads unary operators single operand power

Operational Unary Operators

Operational unary operators handle arithmetic and logical manipulations. They tweak or transform the operand in some way.

an image with the text 3 increment operator + 1 increase the value of a variable
an image with the text 3 increment operator + 1 increase the value of a variable

Here are the main operational unary operators in Java:

Increment and Decrement Operators

List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners

These operators, prefixed with `++` (increment) or `--` (decrement), add or subtract 1 from the operand:

Syntax: `++var` or `--var`

Example (from W3Schools): `int x = 10; x++; System.out.println(x);` will output `11`.

a screenshot of a computer screen with the text 4 decrement operator
a screenshot of a computer screen with the text 4 decrement operator

Negation Operator

The unary negation operator, denoted by `-`, switches the sign of the operand:

Syntax: `-var`

an image of a computer screen with the text, 1 unary plus 4 represents the positive value of a number usually redundant, as numbers are positive by
an image of a computer screen with the text, 1 unary plus 4 represents the positive value of a number usually redundant, as numbers are positive by

Example: `int x = 10; int y = -x; System.out.println(y);` will output `-10`.

Logical Operators

an image of a computer screen with the words 2 unary minus and negative numbers
an image of a computer screen with the words 2 unary minus and negative numbers
a book cover with the words'local operators - making decision'in white letters
a book cover with the words'local operators - making decision'in white letters
This One Mistake Makes Your Array Code Slow
This One Mistake Makes Your Array Code Slow
Java Relational Operators Explained Simply ☕💻 | Compare Values Like a Pro!
Java Relational Operators Explained Simply ☕💻 | Compare Values Like a Pro!
👉 Operators in java 👈
👉 Operators in java 👈
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
the cover of an instruction manual on how to use arrhmetric operators - do the math
the cover of an instruction manual on how to use arrhmetric operators - do the math
Relational Operators in Java beginner friendly and easy to learn 🤩
Relational Operators in Java beginner friendly and easy to learn 🤩
Operators in JAVA
Operators in JAVA
the cover for comparison operators true or false?
the cover for comparison operators true or false?
a book cover with the words assignment operators in action
a book cover with the words assignment operators in action
the cover of what are operators in jawa?, which includes stars and text
the cover of what are operators in jawa?, which includes stars and text
! Logical Operators !
! Logical Operators !
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 𝗶𝗻 𝗖 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲
𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 𝗶𝗻 𝗖 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators
the book cover for bitwise operators for binny pros, which includes an image of
the book cover for bitwise operators for binny pros, which includes an image of
Java Operators Every Beginner Should Know
Java Operators Every Beginner Should Know
The Ternary Operator in JavaScript
The Ternary Operator in JavaScript
Java Operators and its 8 Types that you should know about! - TechVidvan
Java Operators and its 8 Types that you should know about! - TechVidvan

Java provides unary versions of `!` (logical NOT), `~` (bitwise NOT), and `+++` (bitwise NOT with sign extension). They each operate on a single boolean, integer, or binary value:

Syntax: `!var`, `~var`, or `+++var`

Example: `boolean a = true; boolean b = !a; System.out.println(b);` will output `false`.

Casting Unary Operators

Casting unary operators facilitate type conversion. They consist of the desired data type enclosed in parentheses, followed by the operand:

Syntax: `(type) var`

Example: `int x = 10; double y = (double)x;` converts the integer `x` into the double `y`.

Narrowing (Lossy) Conversion

Unary casting can also perform narrowing (lossy) conversions, where data might be lost. This is done at the programmer's risk:

Syntax: `(type) var`

Example (from W3Schools): `double x = 10.5; int y = (int)x;` converts the double `x` into the integer `y`, but the decimal part `0.5` is lost.

Autoboxing and Unboxing

In Java, certain unary operations can implicitly wrap primitive types (like `int`) into objects (`Integer`), or vice versa. This process is called autoboxing or unboxing:

Syntax: Uses mathematical operators (`+`, `-`, `*`, `/`, `%`) with mixed primitives and objects.

Example: `Integer x = 10; Integer y = 20; Integer z = x + y;` autonomously converts `x` and `y` into `int` during the addition.

Mastering unary operators in Java empowers you to write elegant, efficient code tailored to your application's needs. With this knowledge, you're all set to harness W3Schools' further learning resources for Java programming.