运算符
算术运算符
算术运算符:+,-,*,/,%,++,--
public class Demo5 {
public static void main(String[] args) {
//二元运算符 +,-,*,/,%
int a = 12;
int b = 14;
int c = 45;
int d = 78;
System.out.println(a+b);//26
System.out.println(a-b);//-2
System.out.println(a*b);//168
System.out.println(a/b);//0
System.out.println(a%b);//12
System.out.println(a/(double)b);//0.8571428571428571
}
}
public class Demo3 {
public static void main(String[] args) {
//一元运算符 ++ 自增 --自减
int a = 16;
int b = a++; //执行完这段代码后,先给b赋值,再自增
//a = a + 1; //a++ => a = a+1;
System.out.println(a);//17
//a = a + 1; //++a => a = a+1
int c = ++a; //执行完这段代码前,再自增,先给b赋值
System.out.println(a);//18
System.out.println(b);//16
System.out.println(c);//18
//幂运算 2^3 2*2*2 = 8 很多运算我们都会使用一些工具类来操作
double pow = Math.pow(2, 3);
System.out.println(pow);//8.0
}
}
拓展
public class Demo1 {
public static void main(String[] args) {
long a = 12546789545252553L;
int b = 12;
short c = 10;
byte d = 45;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
}
}
关系运算符
关系运算符:<,>,<=,>=,==,!=instanceof
public class Demo2 {
public static void main(String[] args) {
//关系运算符返回的结果为:true false 布尔值
int a = 13;
int b = 76;
System.out.println(a<b);//true
System.out.println(a>b);//false
System.out.println(a==b);//false
System.out.println(a!=b);//true
}
}
逻辑运算符
&&,||,!
public class Demo4 {
public static void main(String[] args) {
//逻辑运算符
//与(and) 或(or) 非(取反)
boolean a= true;
boolean b= false;
System.out.println("a&&b:"+(a&&b));//false //逻辑与运算:两个变量都为真,结果为真
System.out.println("a||b:"+(a||b));//true //逻辑或运算:一个变量为真,结果为真
System.out.println("!(a&&b):"+!(a&&b));//true //如果为真,则变为假,反之亦然
//短路运算
int c = 4;
boolean d = (c>5)&&(c++>10);//前一个结果为false,后一个条件不执行
System.out.println(c);
System.out.println(d);
System.out.println("==============================");
//============================================
boolean e = (c<5)||(c++>10);//前一个结果为true,后一个条件不执行
System.out.println(c);
System.out.println(e);
}
}
位运算符
&,|,^,~,>>,<<
public class Demo6 {
public static void main(String[] args) {
/**
a = 1100 0011;
b = 0011 1010;
--------------------------------------
a&b=0000 0010//两个数比较,如果两个都为1,则为1,否则为0
a|b=1111 1011//两个数比较,如果都为0,则为0,否则为1
~a=0011 1100
a^b=1111 1001//两个数比较,如果两个数相同则为0,不同则为1
==============================================================
2*8 = 2*2*2*2
效率极高!
<< 数字*2
>> 数字/2
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
条件运算符
?,:
public class Demo8 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,那么结果为y,否则为z
int score = 40;
String type = (score>60)?"及格":"不及格";
System.out.println(type);
}
}
扩展赋值运算符
+=,-=,*=,/=
public class Demo7 {
public static void main(String[] args) {
int a = 12;
int b = 8;
//a += b;//a = a+b
//a -= b;//a = a-b
System.out.println(a);
//字符串连接符 + String
System.out.println(""+a+b);
System.out.println(a+b+"");
System.out.println(a+b+""+a+b);
}
}
赋值运算符
=
标签:int,System,运算符,println,public,out From: https://www.cnblogs.com/20030429wym/p/16717572.html