Java一元运算符与示例
运算符构成任何编程语言的基本构建块。 Java也提供了许多类型的运算符,可以根据需要执行各种计算和功能,如逻辑、算术、关系等。它们根据它们提供的功能进行分类。这里有几种类型:
- 算术运算符
- 一元运算符
- 赋值运算符
- 关系运算符
- 逻辑运算符
- 三元运算符
- 位运算符
- 移位运算符
Java中的一元运算符
Java一元运算运算符是只需要一个操作数来执行任何操作(如递增、递减、否定等)的类型。它由对单个操作数进行操作的各种算术、逻辑和其他运算符组成。让我们详细看看各种一元运算符,看看它们是如何操作的。
运算符 1:一元减号(-)
此运算符可用于将正值转换为负值。
句法:
~(operand)
插图:
a = -10
例子:
Java
// Java Program to Illustrate Unary - Operator
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a custom variable
int n1 = 20;
// Printing the above variable
System.out.println("Number = " + n1);
// Performing unary operation
n1 = -n1;
// Printing the above result number
// after unary operation
System.out.println("Result = " + n1);
}
}
Java
// Java Program to Illustrate Unary NOT Operator
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing variables
boolean cond = true;
int a = 10, b = 1;
// Displaying values stored in above variables
System.out.println("Cond is: " + cond);
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
// Displaying values stored in above variables
// after applying unary NOT operator
System.out.println("Now cond is: " + !cond);
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}
Java
// Java program to Illustrate Unary
// Bitwise Complement Operator
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable
int n1 = 6, n2 = -2;
// Printing numbers on console
System.out.println("First Number = " + n1);
System.out.println("Second Number = " + n2);
// Printing numbers on console after
// performing bitwise complement
System.out.println(
n1 + "'s bitwise complement = " + ~n1);
System.out.println(
n2 + "'s bitwise complement = " + ~n2);
}
}
输出
Number = 20
Result = -20
运算符 2: 'NOT' 运算符(!)
这用于将 true 转换为 false,反之亦然。基本上,它反转操作数的逻辑状态。
句法:
!(operand)
插图:
cond = !true;
// cond < false
例子:
Java
// Java Program to Illustrate Unary NOT Operator
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing variables
boolean cond = true;
int a = 10, b = 1;
// Displaying values stored in above variables
System.out.println("Cond is: " + cond);
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
// Displaying values stored in above variables
// after applying unary NOT operator
System.out.println("Now cond is: " + !cond);
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}
输出:
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false
运算符 3:增量(++)
它用于增加整数的值。它可以以两种不同的方式使用:
3.1:后自增运算符
当放置在变量名之后时,操作数的值会增加,但之前的值会暂时保留,直到执行此语句,并在执行下一条语句之前更新。
句法:
num++
插图:
num = 5
num++ = 6
3.2:预增运算符
当放置在变量名之前时,操作数的值会立即递增。
句法:
++num
插图:
num = 5
++num = 6
运算符 4:递减 (–)
它用于减少整数的值。它可以以两种不同的方式使用:
4.1:后减运算符
当放置在变量名之后时,操作数的值会递减,但之前的值会暂时保留,直到执行此语句,并在执行下一条语句之前更新。
句法:
num--
插图:
num = 5
num-- = 4
4.2:预减运算符
当放置在变量名之前时,操作数的值会立即递减。
句法:
--num
插图:
num = 5
--num = 4
运算符 5:按位补码(~)
这个一元运算运算符返回输入值或操作数的反码表示,即所有位反转,这意味着它使每 0 变为 1,每 1 变为 0。
句法:
~(operand)
插图:
a = 5 [0101 in Binary]
result = ~5
This performs a bitwise complement of 5
~0101 = 1010 = 10 (in decimal)
Then the compiler will give 2’s complement
of that number.
2’s complement of 10 will be -6.
result = -6
例子:
Java
// Java program to Illustrate Unary
// Bitwise Complement Operator
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable
int n1 = 6, n2 = -2;
// Printing numbers on console
System.out.println("First Number = " + n1);
System.out.println("Second Number = " + n2);
// Printing numbers on console after
// performing bitwise complement
System.out.println(
n1 + "'s bitwise complement = " + ~n1);
System.out.println(
n2 + "'s bitwise complement = " + ~n2);
}
}
输出:
First Number = 6
Second Number = -2
6's bitwise complement = -7
-2's bitwise complement = 1