Java运算符
2021/7/1 17:38:35
本文主要是介绍Java运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java运算符
Java语言支持如下运算符:优先级
- 算数运算符:+,-,*,/,%,++,--
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=,instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&,|,^,~,>>,<<,>>>(了解!!!)
- 条件运算符:?,:
- 扩展赋值运算符:+=,-=,*=,/=
算术、赋值运算符
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); } }
public class Demo02 { public static void main(String[] args) { Long a = 12211212121L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d); // Long 有一个数为 Long类型,则最终结果为 Long;有一个数为 double,则最终结果为 double System.out.println(b+c+d); // Int System.out.println(c+d); // Int } }
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(b); System.out.println(c); //幂运算 2^3=8 很多运算,我们会使用一些工具操作 double pow = Math.pow(2,3); System.out.println(pow); } }
关系运算符
public class Demo03 { public static void main(String[] args) { // 关系运算符返回的结果:布尔值 正确,错误 int a = 10; int b = 20; int c = 21; // 取余/模运算 System.out.println(c%b);//c/b 21/20=2...1 System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); } }
逻辑运算符
public class Demo05 { public static void main(String[] args) { //与(and) 或(or) 非(取反) 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); // a&&b:若 a为 false则 b不执行直接输出结果为 false System.out.println(d); System.out.println(c); } }
位运算符
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 两个都为 0为 0,否则为 1 A^B = 0011 0001 两个相同为 0,不同则为 1 ~B = 1111 0010 直接取反 2*8 = 16 效率极高!!! << *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); } }
条件运算符
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); } }
扩展赋值运算符
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); //字符串连接符 + System.out.println(a+b); System.out.println(""+a+b);//字符串在左边,则为拼接 System.out.println(a+b+"");//字符串在右边,则为计算 } }
这篇关于Java运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南