基本运算符
JAVA语言支持如下运算符:
-
算术运算符:+ , - , * , / , %(取余、模运算) , ++ , --
-
赋值运算符: =
-
关系运算符:> , < , >= ,<= ,== ,!= ,instanceof(判断其左边对象是否为其右边类的实例,返回boolean类型的数据)
-
逻辑运算符:&& , || ,!
-
位运算符:& , | ,^ , ~ , >> , << , >>>(了解)
-
条件运算符: ?:
-
扩展赋值运算符:+= ,-= , *= ,/=
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);//30
System.out.println(a-b);//-10
System.out.println(a*b);//200
System.out.println(a/b);//0
System.out.println(a/(double)b);//0.5
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12121213213213213L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//12121213213213354 (long)
System.out.println(b+c+d);//141 (int)
System.out.println(c+d);//18 (int)
System.out.println((double) c+d);//18.0 (double)
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果: 布尔值true false
//常与if一起使用
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);//false
System.out.println(a<b);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
System.out.println(c%a);//取余(模运算) 结果为1
}
}
自增自减运算符
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 自减 (一元运算符)
int a = 3;
int b = a++; //a++ a=a+1 (在运算 后 a自增1)
System.out.println(a);//4
int c = ++a; //++a a=a+1 (在运算 前 a自增1)
System.out.println(a);//5
System.out.println(b);//3
System.out.println(c);//5
//幂运算 2^3 2*2*2 = 8 使用工具类操作
double pow = Math.pow(2,3);//求2^3
System.out.println(pow);//8.0
}
}
//-- 自减同理
逻辑运算符
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与&& 或|| 非!
boolean a =true;
boolean b =false;
System.out.println("a && b ="+(a && b));//a && b =false 逻辑与运算:两个变量都为真,结果为真
System.out.println("a || b ="+(a || b));//a || b =true 逻辑或运算:两个变量有一个为真,结果为真
System.out.println("!(a && b) ="+(!(a && b)));//!(a && b) =true //逻辑非运算:如果为真,则变为假;如果为假,则变为真
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);//false
System.out.println(c);//5 c没有自增,发生了短路运算
}
}
位运算
package operator;
public class Demo06 {
public static void main(String[] args) {
/*位运算
A = 0011 1100;
B = 0000 1101;
A&B(与) 0000 1100
A|B(或) 0011 1101
A^B(异或) 0011 0001
~B(取反) 1111 0010
eg:2*8怎样运算最快? 2*8=16 2*2*2*2
<< 左移 *2
>> 右移 /2
*/
System.out.println(2<<3);//16 (左移三位)
//2的二进制: 0000 0010 左移三位变为0001 0000 = 16
}
}
扩展运算符:+= ,-= , *= ,/=
package operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b;//a = a + b
/*
a -= b;//a = a - b
*/
System.out.println(a);//30
//字符串连接符 + ,String
System.out.println(a+b);//50 30+20=50
System.out.println(""+a+b);//3020
System.out.println(a+b+"");//50
}
}
三元运算符 ?:
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 ? "不及格" : "及格";
System.out.println(type);//及格
score = 50;
type = score < 60 ? "不及格" : "及格";
System.out.println(type);//不及格
}
}

浙公网安备 33010602011771号