package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//ctrl+D 复制当前行到下一行
int a =10;
int b =20;
int c =25;
int d =25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12123121212L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d); /*
输出后的值的容量根据计算时高容量的值来取,如a为long,输出值的容量为long*/
System.out.println(b+c+d); // 最高容量的值为int,输出后值容量为int
System.out.println(a+b+c+d); //cast:转换 的意思
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系符运算返回的结果,正确,错误,布尔值。
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println((double) c%a);
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
// ++ -- 自增 自减 一元运算符
int a = 3;
int b = a++; // 执行完这行代码后,先给b赋值,再自增
// a=a+1 (隐藏起来的,不需要写)
System.out.println(a);
// a=a+1(隐藏起来的,不需要写)
int c = ++a; // 执行完这行代码前,先自增。再给b赋值,再自增
System.out.println(a);
System.out.println(a);
System.out.println(b);
System.out.println(b);
System.out.println(c);
System.out.println(c);
//幂运算 2^3(javaz中没有^写法) 2*2*2=8 , 很多运算,我们会使用一些工具来操作。
double pow = Math.pow(3,2);
System.out.println(pow);
}
}
package operator;
public class Demo05 {
public static void main(String[] args) {
// 与(我和你)或(我或者你 ) 异或 (不是我 就是你) 非(取反)
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 = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(c); // c++未运行
boolean f = (c++<4)&&(c<4);
System.out.println(d);
System.out.println(f);
System.out.println(c); //c++运行了
}
}
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A=0011 1100
B=0000 1101
A&B = 0000 1100 (与)全对才为1
A|B = 0011 1101 (或) (只需要一个正确即为1)全对可以
A^B = 0011 0001 (异或) (只有一个正确才为1)全对不行
~B = 1111 0010 (非) 真变假,假变真
2*8=16 2*2*2*2*2
效率极高!!!
<< *2 <<即1在二进制中向左移一位
>> /2 >>即1在二进制中向右移一位
0000 0000 0
0000 0001 1 1
0000 0010 2 2*1 0000 0011 3 2*1+1
0000 0100 4 2*2*1
0000 1000 8 2*2*2*1
0001 0000 16 2*2*2*2*1
0010 0000 32 2*2*2*2*2*1
0100 0000 64 2*2*2*2*2*2*1
1000 0000 128 2*2*2*2*2*2*2*1
*/
int b =2;
System.out.println(2<<3); //2右移3位
int a = 2;
double pow =Math.pow(2,8);
System.out.println(pow);
}
}
package operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
System.out.println(a);
a-=b; //a = a-b
System.out.println(a);
//字符串连接符 + ,string
System.out.println(""+a+b); //1020,“”号后,即字符串连接符后+String,即将其串联起来
System.out.println(a+b+""); //30,前面仍旧会运算
}
}
package operator;
public class Demo08 {
//三元运算符
public static void main(String[] args) {
//x ? y : z
//如果x等于true,则结果位y。否则结果位z
int score = 80;
String type = score <60 ?"不及格":"及格";//必须掌握
//if
System.out.println(type);
}
}