运算符
java语言支持如下运算符:
算术运算符

注意事项:
/和%的区别:两个数据做除法,/取结果的商,%取结果的余数。
整数操作只能得到整数,要想得到小数,必须有浮点数参与运算
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D :复制当前行到下一行
int a = 10;
int b = 20;
int c = 22;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
//取余,模运算
System.out.println(c%a); // c / a 22 / 10 = 2...余2
}
}
public class Demo2 {
public static void main(String[] args) {
long a = 123454347568L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d); //Long
System.out.println(b+c+d); //int
System.out.println(c+d); //int
}
}
++,--(一元运算符)
|
符号 |
说明 |
|
++,自增 |
++在左边,先++,后赋值 ++在右边,先赋值,后++ |
|
--,自减 |
--在左边,先--,后赋值 --在右边,先赋值,后-- |
public class Demo4 {
public static void main(String[] args) {
// ++ -- 自增,自减,一元运算符
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,再自增
//隐藏代码 a++ a = a + 1;
System.out.println(a);
//隐藏代码 ++a a = a + 1;
int c = ++a; //执行完这行代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//扩展:幂运算 2^3 2*2*2 = 8 很多运算,我们会使用一些工具类来操作!
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
赋值运算符

public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; // a = a+b
System.out.println(a); //30
a-=b; // a = a-b
System.out.println(a); //10
//字符串连接符 + ,String
System.out.println(""+a+b); //1020 ""字符串在前面,会拼接 所以是1020
System.out.println(a+b+""); //1020 ""字符串在后面,会运算后再拼接 所以是30
}
}
关系运算符:>,< ,>= ,<= ,== ,!=, instance of

注意事项:
关系运算符的结果都是boolean类型,要么是true,要么是false。
千万不要把“==”误写成“=”。
public class Demo3 {
public static void main(String[] args) {
//关系运算符的结果都是boolean类型,要么是true,要么是false。
int a = 10;
int b = 20;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
逻辑运算符:&&,||,!
|
符号 |
作用 |
说明 |
|
&& |
逻辑与 |
两个变量都为真,结果才为true |
|
|| |
逻辑或 |
两个变量有一个为真,则结果才为true |
|
! |
逻辑非 |
如果是真,则变为假,如果是假则变为真 |
//逻辑运算符
public class Demo5 {
public static void main(String[] args) {
// 与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b)); //逻辑与:两个变量都为真,结果才为true
System.out.println("a || b:"+(a||b)); //逻辑或:两个变量有一个为真,则结果才为true
System.out.println("!(a && b):"+!(a&&b)); //逻辑非:如果是真,则变为假,如果是假则变为真
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(c);
System.out.println(d);
//不理解 敲十遍就理解了
}
}
位运算符:&,|,^,~,>>,<<(了解即可!!!)

public class Demo6 {
public static void main(String[] args) {
/* 位运算
A = 0011 1100
B = 0000 1101
-------------------------
A&B = 0000 1100 // A与 B 上下比较,两个都为 1 才为 1 否则就是 0
A|B = 0011 1101 // A或 B 上下比较 两个都为 0 才为 0 否则就是 1
A^B = 0011 0001 // A亦或B 上下比较 两个位置相同 则为0 否则为 1
~B = 1111 0010 // ~B取反 上下比较 取反即可
-----------------------------------------
2*8 = 16 2*2*2*2
位运算效率极其高!!!
<< 左移 *2
>> 右移 /2
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); //16 左移3位
System.out.println(16>>3); //2 右移3位
}
}
三元运算符 ?:
public class Demo8 {
public static void main(String[] args) {
// x ? y : z
// 如果 x==true,则结果为 y,否则结果为 z
// 必须掌握
int score = 80;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}
本文详细介绍了Java中的各种运算符,包括算术、关系、逻辑、位运算符以及三元运算符的用法,并通过实例展示了它们在实际编程中的应用。强调了/和%的区别,自增和自减运算符的两种形式,以及逻辑运算符的短路特性。此外,还提到了位运算的高效性以及三元运算符的条件判断功能。
2279

被折叠的 条评论
为什么被折叠?



