运算符
运算符
算数运算符
-
加减乘除 +,-,*,/
-
取余%(模运算),++,--
-
赋值运算符=
-
幂运算,使用数学工具类 a^b=Math.pow(a,b); //double c=Math.pow(a,b)必须使用double定义否则报错
关系运算符
-
小于:<, 大于:> , 大于等于:>=,小于等于<=,不等于!= instance of
逻辑运算符
-
与&& 或 || 非 !
了解!!!!!!!!!!!!!:
位运算符(与计算机操作有关)
-
与:& , 或:|,亦或: ^,非: ~,右移 >>, 左移:<<, >>>
条件运算符
-
? ://a?b:c a为true则输出b,否则输出c
拓展赋值运算符
-
+= -= *= /= //例子a+=b 是a=a+b 其余同理
代码
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//ctrl+D:复制当前一行
int a=10;
int b=20;
int c=30;
int d=40;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);//作用域在整数0.5是浮点数,变为0
System.out.println(a/(double)b);//转换一个变成浮点型得到0.5
}
}
结果:
30
-10
200
0
0.5
public class Demo02 {//cast:转换
public static void main(String[] args) {
long a=123321654;
int b=999;
short c=20;
byte d=6;
System.out.println(a+b+c+d);//L型
System.out.println(b+c+d);//int型
System.out.println(c+d);//除L型外其余皆是int型
System.out.println(b%c);//取余 999/20=49....19
}
}
结果:
123322679
1025
26
19
ublic class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:true or false boolean值
int a=10;
int b=20;
int c=50;
int d=50;
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);
}
}
结果:
false
true
false
true
false
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 自减 为一元运算符
int a=6;
int b=a++;//执行完这行代码后,先给b赋值,之后再自增
//a=a+1省略了这一行
System.out.println(a);
int c=++a;//执行完这行代码后,先给自增,之后再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 借助数学工具类Math
double e=Math.pow(2.5,5);
System.out.println(e);
}
}
结果:
7//a
8//a
6//b
8//c
97.65625//e
public class Demo05 {
public static void main(String[] args) {
//与and 或or 非(取反)
boolean a=true;
boolean b=false;
System.out.println("a && b:"+(a&&b));
System.out.println("a || b:"+(a || b));
System.out.println("!a && b:"+(!a && b ));
//短路运算
int c=4;
boolean d=(c>3)&&(c++>4);//此时c依旧是4,执行完这段代码后才是5
System.out.println("d="+d);
System.out.println("c="+c);
}
}
结果:
a && b:false
a || b:true
!a && b:false
d=false
c=5
public class Demo06 {
public static void main(String[] args) {
/*
位运算 效率高
A=0010 1100
B=0000 1101
A&B=0000 1100 与运算 每一对应位进行对比 0&0=0 1&0=0 1&1=1
A|B=0010 1101 或运算 每一对应位进行对比 0|0=0 1|0=1 1|1=1
A^B=0010 0001 亦或 相同取0 不同取1
~B=1111 0010 非运算 取反
左移<< 向左移动
右移>> 向右移动
2*8=16 2*2*2*2=16
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);//2^1->2^(1+3) 在二进制下进行
System.out.println(3<<3);//0011 向左边移动三位 0001 1000 即2^4+2^3=24
System.out.println(2>>>1);
}
}
结果:
16
24
1
public class Demo07 {
public static void main(String[] args) {
int a=10;
int b=20;
a+=b;//+=释义:a=a+b,a=30
System.out.println(a);
a-=b;//a=a-b 30-20
System.out.println(a);
//字符串连接符 +,String
System.out.println(""+a+b);//先转化位字符串再组合在一起
System.out.println(a+b+"");//先相加再转化成字符串
//三元运算符
//x ? y : z
//如果x为true ,则结果为y,否则结果为z
int c=60;
String ans=c>=60?"及格":"不及格";//必须掌握
System.out.println(ans);
}
}
结果:
30
10
1020
30
及格

浙公网安备 33010602011771号