📅  最后修改于: 2020-03-24 02:27:06             🧑  作者: Mango
Java提供了许多类型的运算符,可以根据需要使用它们。根据提供的功能对它们进行分类。其中一些类型是:
让我们详细了解一下它们。
// Java展示算术运符
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
String x = "芒果", y = "文档";
// + and - operator
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
// + operator if used with strings
// concatenates the given strings.
System.out.println("x + y = " + x + y);
// * and / operator
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
// modulo operator gives remainder
// on dividing first operand with second
System.out.println("a % b = " + (a % b));
// if denominator is 0 in division
// then Arithmetic exception is thrown.
// uncommenting below line would throw
// an exception
// System.out.println(a/c);
}
}
输出:
a + b = 30
a - b = 10
x + y = 芒果文档
a * b = 200
a / b = 2
a % b = 0
// Java展示一元运算符
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
boolean condition = true;
// 前递增
// 先计算a = a+1 然后 c = a;
c = ++a;
System.out.println("c (++a) = " + c);
// 后递增
// 先计算c=b, 然后 b=b+1
c = b++;
System.out.println("c (b++) = " + c);
// 先递减
// 先计算d=d-1 然后 c=d
c = --d;
System.out.println("c (--d) = " + c);
// 后递减
// 先计算c=e 然后 e=e-1
c = e--;
System.out.println("c (e--) = " + c);
// not运算符
System.out.println("!condition ="
+ !condition);
}
}
输出:
c (++a) = 21
c (b++) = 10
c (--d) = 19
c (e--) = 40
!condition =false
variable = value;
在许多情况下,赋值运算符可以与其他运算符结合使用,以构建称为复合语句的较短版本的语句。例如,我们可以写一个+ = 5 代替a = a + 5。
int a = 5;
a += 5; //a = a+5;
// Java程序,展示赋值运算符
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c, d, e = 10, f = 4, g = 9;
// 赋值运算
c = b;
System.out.println("c = " + c);
// 如下会报错,因为右侧值在赋值之前,必须被初始化,否则编译错误
// c = d;
// 赋值运算
a = a + 1;
b = b - 1;
e = e * 2;
f = f / 2;
System.out.println("a, b, e, f = " + a + ", "
+ b + ", " + e + ", " + f);
a = a - 1;
b = b + 1;
e = e / 2;
f = f * 2;
// 简短运算符
a += 1;
b -= 1;
e *= 2;
f /= 2;
System.out.println("a, b, e, f ("
+ "简短运算符)= "
+ a + ", " + b + ", "
+ e + ", " + f);
}
}
输出:
c = 10
a, b, e, f = 21, 9, 20, 2
a, b, e, f (简短运算符)= 21, 9, 20, 2
variable 关系运算符 value
一些关系运算符是:
// Java程序展示关系运算符
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10;
String x = "Thank", y = "Thank";
int ar[] = { 1, 2, 3 };
int br[] = { 1, 2, 3 };
boolean condition = true;
// 各种关系运算符例子
System.out.println("a == b :" + (a == b));
System.out.println("a < b :" + (a < b));
System.out.println("a <= b :" + (a <= b));
System.out.println("a > b :" + (a > b));
System.out.println("a >= b :" + (a >= b));
System.out.println("a != b :" + (a != b));
// Arrays不能使用观念运算符
System.out.println("x == y : " + (ar == br));
System.out.println("condition==true :"
+ (condition == true));
}
}
输出:
a == b :false
a < b :false
a <= b :false
a > b :true
a >= b :true
a != b :true
x == y : false
condition==true :true
// Java展示逻辑运算符
import java.util.*;
public class operators {
public static void main(String[] args)
{
String x = "Sher";
String y = "Locked";
Scanner s = new Scanner(System.in);
System.out.print("输入用户名:");
String uuid = s.next();
System.out.print("输入密码:");
String upwd = s.next();
// 检查用户名和密码是否匹配.
if ((uuid.equals(x) && upwd.equals(y))
|| (uuid.equals(y) && upwd.equals(x))) {
System.out.println("欢迎用户.");
}
else {
System.out.println("密码或用户名错误");
}
}
}
输出:
输入用户名:Sher
输入密码:Locked
欢迎用户.
condition ? if true : if false
上面的语句表示如果条件的计算结果为true,则在’?’之后执行语句 否则在’:’之后执行语句。
// Java展示三元运算符
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 30, result;
// result被赋值三个当中最大值
result = ((a > b)
? (a > c)
? a
: c
: (b > c)
? b
: c);
System.out.println("三个当中最大值 = "
+ result);
}
}
输出:
三个当中最大值 = 30
// Java按位运算符
public class operators {
public static void main(String[] args)
{
// 如果int a = 010
// Java把a在底层作为八进制,以0开始
int a = 0x0005;
int b = 0x0007;
// 按位与
// 0101 & 0111=0101
System.out.println("a&b = " + (a & b));
// 按位与
// 0101 | 0111=0111
System.out.println("a|b = " + (a | b));
// 按位xor
// 0101 ^ 0111=0010
System.out.println("a^b = " + (a ^ b));
// 按位与
// ~0101=1010
System.out.println("~a = " + ~a);
// 也可以使用简短表达
// a=a&b
a &= b;
System.out.println("a= " + a);
}
}
输出:
a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5
number shift_op number_of_places_to_shift;
<<:左移运算符:将数字的位向左移,在结果左的空白处填充0。与将数字乘以2的乘方相似的效果。
>>:带符号的右移运算符:将数字的位向右移,在结果空白处填充0。最左边的位取决于初始编号的符号。将数除以二的幂具有相似的效果。
>>>:无符号右移运算符:将数字的位向右移,在结果左边的空白处填充0。最左边的位设置为0。
// Java 移位运算符
public class operators {
public static void main(String[] args)
{
int a = 0x0005;
int b = -10;
// 左移运算符
// 0000 0101<<2 =0001 0100(20)
// 等效于 5*(2^2)
System.out.println("a<<2 = " + (a << 2));
// 右移运算符
// 0000 0101 >> 2 =0000 0001(1)
// 等效于5/(2^2)
System.out.println("a>>2 = " + (a >> 2));
// 无符号右移运算符
System.out.println("b>>>2 = " + (b >>> 2));
}
}
输出:
a<<2 = 20
a>>2 = 1
b>>>2 = 1073741821
object instance of class/subclass/interface
// Java操作符实例
class operators {
public static void main(String[] args)
{
Person obj1 = new Person();
Person obj2 = new Boy();
// obj1是person类, 它不是Boy的对象或接口
System.out.println("obj1是Person对象: "
+ (obj1 instanceof Person));
System.out.println("obj1是Boy对象: "
+ (obj1 instanceof Boy));
System.out.println("obj1是MyInterface对象: "
+ (obj1 instanceof MyInterface));
// 因为obj2是Boy类,其父类是person,它实现了Myinterface接口,它是这两个类的对象
System.out.println("obj2是Person对象: "
+ (obj2 instanceof Person));
System.out.println("obj2是Boy对象: "
+ (obj2 instanceof Boy));
System.out.println("obj2是MyInterface对象: "
+ (obj2 instanceof MyInterface));
}
}
class Person {
}
class Boy extends Person implements MyInterface {
}
interface MyInterface {
}
输出:
obj1是Person对象: true
obj1是Boy对象: false
obj1是MyInterface对象: false
obj2是Person对象: true
obj2是Boy对象: true
obj2是MyInterface对象: true
运算符的优先级和关联性
当处理涉及一种以上类型算子的混合方程时,将使用优先规则和关联规则。在这种情况下,这些规则将确定首先考虑方程式的哪一部分,因为同一方程式可能会有许多不同的求值方法。下表按大小降序描述了运算符的优先级,其幅度最高,最高的代表最低,最低的代表最低。
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
// 运算符优先级.
// (* = / = %) > (+ = -)
// 打印 a+(b/d)
System.out.println("a+b/d = " + (a + b / d));
// 如果优先级相同,以下规则将被遵循
// e/f -> b*d -> a+(b*d) -> a+(b*d)-(e/f)
System.out.println("a+b*d-e/f = "
+ (a + b * d - e / f));
}
}
输出:
a+b/d = 20
a+b*d-e/f = 219
public class operators {
public static void main(String[] args)
{
int a = 20, b = 10, c = 0;
// a=b+++c 被编译成
// b++ +c
// a=b+c 然后 b=b+1
a = b++ + c;
System.out.println("a(b+c), "
+ " b(b+1), c = "
+ a + ", " + b
+ ", " + c);
// a=b+++++c 被编译成
// b++ ++ +c
// 这会报错.
// a=b+++++c;
// System.out.println(b+++++c);
}
}
public class operators {
public static void main(String[] args)
{
int x = 5, y = 8;
// 链接 x、y
// 首先 x 被加入 "concatenation (x+y) = "
// 产生 (x+y) = 5"
// 然后 8再被接入.
System.out.println("Concatenation (x+y)= "
+ x + y);
// x、y的加法
System.out.println("Addition (x+y) = "
+ (x + y));
}
}
输出:
Concatenation (x+y)= 58
Addition (x+y) = 13