1、赋值操作符
= 赋值
eg:
int cadence = 0;
int speed = 0;
int gear = 1;
2、基本数学运算符
+ 加 (两边是数值型变量或值作数学运算,其中一个为字符型变量或值作连接运算)
- 减
* 乘
/ 除 (两个整数作除法,结尾取整,原文:Integer division rounds toward 0)
% 取余
3、一元运算符
+ 表示正数
- 表示负数
++ 自增运算 每次加1。在变量之前表示先+1,再使用该变量;在变量之后表示使用该变量后再+1
-- 自减运算 每次减1。在变量之前表示先-1,再使用该变量;在变量之后表示使用该变量后再-1
!逻辑运算符,取反
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
4、关系运算符
== 判断相等(基本类型根据值作对比,判断是否相等;引用类型根据对象引用的地址判断是否相等)
!= 判断不等于(同上)
> greater than
>= greater than or equal to
< less than
<= less than or equal to
以上关系运算符通常用作基本类型的比较,对象的比较通常使用对象的equals方法。
5、条件运算符
&& 且
|| 或
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
6、类型检测
instanceof
用于判断一个实例是否是某个类(某个类或某个类的子类或某个接口)的实例。
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: "
+ (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: "
+ (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: "
+ (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: "
+ (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
输出:
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
7、位移运算符
<< 左移运算
>> 右移运算
>>> 无符号右移运算
& 按位与
^ 按位异或
| 按位或
~ 按位取反(非)