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.

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.

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

Here are the main operational unary operators in Java:
Increment and Decrement Operators

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`.

Negation Operator
The unary negation operator, denoted by `-`, switches the sign of the operand:
Syntax: `-var`

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




















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.