首页 > Java > java教程 > 正文

Java 中的一元运算符

PHPz
发布: 2024-08-30 15:19:07
原创
1029 人浏览过

对于任何编程语言来说,都有大量的运算符、方法和函数可供根据需要使用。基于类、面向对象的编程语言 Java 提供了多种运算符,Java 中的一种运算符就是“一元运算符”。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

一元运算符可以是只接受一个操作数并执行将值递增或递减 1 的简单工作的运算符。此外,一元运算符还对表达式进行取反运算,布尔值可以取反。

一元运算符的类型

有五个一元运算符能够执行各种操作。下面给出了五个一元运算符的列表:

  • 一元加号,用“+”表示。
  • 一元减号,用“-”
  • 表示
  • 一元自增运算符,用“++”表示。
  • 一元自减运算符,用“–”表示
  • 逻辑补码运算符,用“!”表示

一元运算符与二元运算符有很大不同,二元运算符接受两个操作数。这些运算符就像用于对操作数执行某些操作的特殊符号;这里的操作数是变量和值。

Java 中的一元运算符

1.一元加

简单地返回正值。无论值是什么,一元加都不会返回负数形式。

2.一元减号

就像加运算符返回正值一样,一元减运算符返回相同值的负形式。对于上面解释的一元运算符,我们将演示一个示例,其中我们将实现一元加号和减号运算符。

代码:

class unary_ops {
public static void main(String[] args) {
int num = 6;
num = +num;
System.out.println(num);
num = -num;
System.out.println(num);
}
}
登录后复制

代码解释:我们在上面的示例中演示了加号和减号一元运算符。我们有我们的类,然后是主类,并且我们声明了一个值为 6 的简单整数。然后我们将 num 分配给一元加运算符。然后我们打印了结果,这将是简单的纯 6。然后我们将相同的变量传递给一元减运算符,并且这里的值发生变化。我们已经用 print 语句打印了输出,预计为 -6,表示负 6。执行上述代码后,6 和 -6 是预期输出。

输出:

Java 中的一元运算符

3.一元自增运算符

顾名思义,这个一元运算符所做的就是将值加1的操作。无论变量的值是多少,通过增量运算符传递后,值都会加1。一元增量运算符可以是后来根据增量操作发生的时间分为两种类型:

  • 后递增:值首先被处理,然后递增。后自增时,无论值是多少,都会先用于计算,然后再加一。
  • 预自增:相反,预自增是先自增,然后对增量后的值进行计算。

4.一元自减运算符

就像增量运算符将值加一一样,一元减量运算符将变量值减 1。

与自增运算符类似,自减运算符有两种变体:

  • Post-Decrement: 使用后置形式的减量运算符,首先使用该值,然后更新该值。
  • 预减: 采用前缀形式,值先减一,然后用于任何计算操作。

演示上述自增和自减运算符的使用。

代码:

class unary_ops {
public static void main(String[] args) {
int num = 6;
num--;
System.out.println(num);
num++;
System.out.println(num);
}
}
登录后复制

代码解释: 与主类相同的类,整数num,值为5。首先,我们将减量运算符传递给变量,作为num——并且将打印该值。稍后我们将相同的计算值传递给增量运算符,结果将被打印。我们的原始值为 6,执行后输出将为“5 6”。它会先减到 5,然后加 1,再次回到 6。

输出:

Java 中的一元运算符

5.逻辑补运算符

该运算符用于反转任何变量的布尔值。前任。如果变量的布尔值为 true,则在使用逻辑运算符传递后,它将被反转为 false。

Code:

class unary_ops {
public static void main(String[] args) {
boolean bvalue = false;
System.out.println(bvalue);
System.out.println(!bvalue);
}
}
登录后复制

Code Interpretation: We demonstrated a Logical Complement operator using the Boolean data type. In our class, we have the main class within and our simple boolean variable, which holds the value of false. In our first print statement, we printed the original value and later passed the logical complement operator; as you can see, we’ve used the “!” symbol. This implementation will invert the value of the boolean variable, resulting in a changed output of true.

Output:

Java 中的一元运算符

Below are the cases, which if executed, will result in errors:

  • It is important to understand that these increment and decrement operators can only be used with variables, not constant values. Implementing these operators with contact values will result in an error. Another interesting point is how an unexpected error will occur if you use these operators in nested form. Not acceptable nested form is: int x = ++(++y);
  • Operations on Final Variables: Declaring a final variable is as easy as adding a final keyword before the data type, which results in a type variable whose value cannot be changed. Keeping this in mind, we cannot implement these operators on the final variable, as these operations result in a change in value.
  • For Boolean values: We can implement these operators on all types of primitive data types except Boolean values.
  • In case of any of the scenarios mentioned above, it will result in errors.

Conclusion

There are 5 unary operators and with pre and post as two varieties. We understood each operator with a specific definition and usage. Along with an explanation, we have programs for respective operators, screenshots, and code interpretation. And some essential tips to wisely implement these operators.

以上是Java 中的一元运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!