Java中的运算符
- 算术运算符:+,-,*,/,%(取余),++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=,instanceof
- 逻辑运算符:&&,||,!(与或非)
- 位运算符:&,|,^,~,>>,<<,>>>(了解!!)
- 条件运算符:?:
- 扩展赋值运算符:+=,-=,*=,/=
二元运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
// 二元运算符
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); //运行结果为0 int 0.5直接取0 需要强转
System.out.println(a/(double)b); //运行结果为0.5
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 1231241231212L;
int b = 123;
short c = 10;
byte d = 8;
double f = 3.444;
long e = 34L;
/*
数据类型由低到高排序
byte--short--char--int--long--float--double
小数优先级一定大于整数的原则,即在运算过程中如果有多种数据类型,则结果类型一定是最高优先级
需要注意:int及以下数据类型运算时,运行结果一定是int类型
*/
// 在运算过程中:double类型+long类型,则这个运行结果类型也为double
System.out.println(f+e); // 运行结果为37.444 double类型
// 在运算过程中,long类型+int类型+short类型+byte类型,则这个运行结果类型为long
System.out.println(a+b+c+d); // 运行结果1231241231353 long类型
// 在运算过程中,int类型+short类型+byte类型,则这个运行结果类型为int
System.out.println(b+c+d); //运行结果为141 int类型
// 在运算过程中,short类型+byte类型,则这个运行结果类型为int
System.out.println(c+d); // 运行结果为14 int类型
/*
小tips:如何查看运行结果类型
将类型强制转换为String类型(代码如下) 会有报错提示
查看报错提示为Inconvertible types; cannot cast 'double' to 'java.lang.String'
即代表该运算结果类型为double
*/
System.out.println((String)(f+e));
}
}
一元运算符
- a++运算:先赋值,再自增
package operator;
public class Demo03 {
public static void main(String[] args) {
// 关于a++ 自增 一元运算符:代表 a=a+1
int a = 3;
int b = a++; // 这行代码的意思是,先给b赋值,再自增(a=a+1):即运行完这行代码,b变成了3,a加了1变成了4
System.out.println(a); // 运行结果为4
System.out.println(b); // 运行结果为3
}
}
- ++a运算:先自增,再赋值
package operator;
public class Demo04 {
public static void main(String[] args) {
// 关于++a 自增 :代表 a=a+1
int a = 3;
// 这行代码的意思是,先进行自增(a=a+1),再给b赋值:即运行完这行代码,a变成了4,b随即也被赋值成为了4
int b = ++a;
System.out.println(a); // 运行结果为4
System.out.println(b); // 运行结果为4
}
}
幂运算(暂时了解)
// 幂运算 例如2^3=8 使用Math工具类
double pow = Math.pow(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:"+(b&&a)); // 逻辑与运算 运行结果为false
System.out.println("a || b:"+(a||b)); // 逻辑或运算 运行结果为true
System.out.println("!(a && b):"+!(a&&b)); // 运行结果为true
/*
短路运算 :下边这行代码,b本身就已经是false,则计算机不会再去判断后边的a是否为真
System.out.println("a && b:"+(b&&a));
以下代码即可证明计算机为短路运算
*/
int c = 5;
// c++如果运行了,则是先自增变为6; 但是第24行代码输出c的值为5,则证明计算机没有计算运算符后边的值
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
位运算符
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
-----------------------------------------------------
A&B = 0000 1100 如果两个数都是1,结果才为1,否则为0
A|B = 0011 1101 只要有一个1,结果就为1,否则为0
A^B = 0011 0001 如果两个数相同,则结果为0,否则为1
~B = 1111 0010 完全取相反的值
*/
/*
常见面试题:2*8如何计算最快?
<< 左移 <<1:代表乘1个2 <<2:代表乘2个2 <<3:代表乘3个2
>> 右移 >>1:代表/2 >>2:代表/2个2 >>3:代表/3个2
0000 0000 0
0000 0001 1
0000 0010 2 从2开始左移(移1的位置)
0000 0011 3
0000 0100 4 左移1位得到4
0000 1000 8 左移2位得到8
0001 0000 16 左移3位得到16
*/
System.out.println(2<<3); // 代表2乘3个2得到16
}
}
扩展赋值运算符
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); //运行结果为30
/*
字符串连接符 +
只要在运算过程中先出现String类型,就会将后边类型转换为String再进行连接,如16行代码输出结果
如果在运算过程中先是运算,再是String,则会先进行计算,如17行代码输出结果
*/
System.out.println(""+a+b); //运行结果为1020,将两个数字连接起来了
System.out.println(""+a+b); //运行结果为30,将两个数字连接起来了,但仍然是String类型
}
}
条件运算符
package operator;
// 三元运算符
public class Demo08 {
public static void main(String[] args) {
/*
x ? y : z
如果x为真,则结果为y,否则为z
*/
int score = 80;
String type = score <60? "不及格" : "及格";
System.out.println(type); // 运行结果为及格
}
}
本文详细介绍了Java中的各种运算符,包括算术运算符如+,-,*,/,%;逻辑运算符如&&,||,!;位运算符如&,|,^,~,>>,<<,>>>;以及扩展赋值运算符如+=,-=,*=,/=等。还探讨了一元运算符的使用,如++,--的前后置区别,并提及了条件运算符。
1962

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



