算术运算符
package com.luke.ch02_operator;
public class MathDemo {
public static void main(String[] args) {
//初始化变量
int a=20;
int b=3;
//算术运算符+-*/%
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b); //不能整除的时候,得到也是商,不会是小数
System.out.println(a%b);
//自增++和自减--
a++;
System.out.println(a);
++a;
System.out.println(a);
int c = b++; //b先赋值给c,再自增
System.out.println(c);
int d = ++b; //b先自增再赋值给d
System.out.println(d);
}
}
关系运算符
package com.luke.ch02_operator;
public class RelationDemo {
public static void main(String[] args) {
char a='A';
int b=69;
int c=65;
System.out.println(a==c);
System.out.println(a>b);
System.out.println(a!=c);
System.out.println(a<c);
System.out.println(a>=c);
System.out.println(b<=a);
}
}
逻辑运算符
package com.luke.ch02_operator;
public class LogicDemo {
public static void main(String[] args) {
boolean a=true;
boolean b=false;
int c=0;
int d=1;
char e='A';
System.out.println(a&b);
System.out.println(a|b);
System.out.println(!b);
System.out.println(c&d);//直接进行与运算,转换为二进制
System.out.println(c|e);//e先转换为int类型,然后都转换为二进制
}
}
赋值运算符
package com.luke.ch02_operator;
public class AssignDemo {
public static void main(String[] args) {
int a=10;
int b=10;
int c=10;
int d=10;
int e =10;
a += 2;
System.out.println(a);
b -=2;
System.out.println(b);
c *= 2;
System.out.println(c);
d /= 4;
System.out.println(d);
e %= 3;
System.out.println(e);
}
}