什么是编程中常用的运算符及其功能?

2026-05-06 00:031阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1081个文字,预计阅读时间需要5分钟。

什么是编程中常用的运算符及其功能?

运算符及其优先级(注意运算符的优先级):/*,%,/*,%+,-+,-规则:* 根据优先级,从左至右计算。* 操作数中无int类型,结果为int。* 操作数优先级最高者为int,结果为int。* 操作数中()括号优先计算。

运算符(注意运算的优先级)

/* 优先级低-->高byte short char int long float double * 规律: * 根据优先级: * 1.操作数中无int类型,结果为int * 2.操作数中优先级最高的为int,结果为int * 3.操作数中类型优先级大于int的 ,结果类型为操作数中最高的类型 * */ /* * System.out.println((String)(c+d));//报错 cast 'int' to 'java.lang.String' * 可以前面加String 根据提示来看是什么类型。 * */ 代码演示

public class Demo05 { public static void main(String[] args) { byte b = 8; short s = 10; char c = '2'; int i = 23; long l = 211321312312L; float f = 21.3f; double d = 21.5; //变量类型不同进行运算 System.out.println((b + s));//byte + short = int System.out.println((b + c));//byte + char = int System.out.println((b + i));//byte + int = int System.out.println((b + l));//byte + long =long System.out.println((b + f));//byte + float = float System.out.println((b + d));//byte + double = double } }


1. 算术运算符

+ - * / % ++ -- 2. 赋值运算符

= 扩展赋值运算符: += -= *= /= 代码演示

public class Demo07 { public static void main(String[] args) { int a = 10; int b = 20; a += b; //a = a + b; System.out.println(a); a -= b; //a = a - b; //注意此时a为30 30-20 = 10 System.out.println(a); System.out.println("--------------------------------------------"); //字符串连接符 + int c = 10; int d = 20; System.out.println("cd的和为:" + c + d); //面试题 System.out.println("ddd" + c + d);//前面有String类型 就会把后面得 c 和 d 都转化为String类型 System.out.println(c + d + "ddd");//这是后面有String类型 先c+d得出结果30 然后与后面的String类型连接 System.out.println("Hello World !"); } } 3. 关系运算符

> < >= <= == != 代码演示

public class Demo03 { public static void main(String[] args) { 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); System.out.println(a = b); //赋值 System.out.println(a != b); //返回值为布尔类型 false true //true System.out.println(c % a); //1 System.out.println("Hello World !"); } } 4. 逻辑运算符

&& || ! 与 && and 或 || or 非 ! 取反 短路运算

public class Demo05 { public static void main(String[] args){ boolean a = true; boolean b = false; System.out.println(a && b); System.out.println(a || b); System.out.println(!(a && b)); //短路运算 int c = 5; boolean d = (c < 4) && (c++ < 4); System.out.println(d); System.out.println(c); /* * c的值没有变为6 仍为5 * 是因为boolean d = (c < 4) && (c++ < 4); 判断(c <4 )为假 直接输出结果为假 没有执行 (c++ < 4) * */ System.out.println("--------------------"); int c1 = 3; boolean d1 = (c1++ < 4);// 先3<4 true 后 c1= c1+1=4 System.out.println(d1); System.out.println(c1); int c2 = 3; boolean d2 = (++c2 < 4);// 先c2自加为4 后比较 false System.out.println(d2); System.out.println(c2); System.out.println("Hello World"); } } 5. 位运算 有面试题

& | ^ ~ >> << >>>(了解!!!) 代码演示

public class Demo06 { public static void main(String[] args) { /* 位运算*** A = 0011 1100 B = 0000 1101 -------------------------------------------- A&B 0000 1100 (A与B 对应位都为1得1 有0得0) A|B 0011 1101 (A或B 对应位都为0得0 有1得1) A^B 0011 0001 (取反/异或 对应位相同得0 不同得1) ~B 1111 0010 (B对应位为1得0 为0得1 都取反) 问2 * 8 = 16 计算机怎么算最快 2 * 2 * 2 * 2 = 16 */ /* << 左移 相当于把数字*2 >> 右移 相当于把数字/2 2 << 3 0000 0010 对应十进制2 1左移三位 0001 0000 对应十进制16 */ System.out.println(2 << 3);//2*2*2*2 System.out.println("Hello World !"); } } 6. 条件运算符(三元运算符)

? : x ? y : z 当x为true 则结果为y 否则结果为z 代码演示

//三元运算符 条件运算符 ? : //必须会 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); System.out.println("Hello World !"); } }

什么是编程中常用的运算符及其功能?

本文共计1081个文字,预计阅读时间需要5分钟。

什么是编程中常用的运算符及其功能?

运算符及其优先级(注意运算符的优先级):/*,%,/*,%+,-+,-规则:* 根据优先级,从左至右计算。* 操作数中无int类型,结果为int。* 操作数优先级最高者为int,结果为int。* 操作数中()括号优先计算。

运算符(注意运算的优先级)

/* 优先级低-->高byte short char int long float double * 规律: * 根据优先级: * 1.操作数中无int类型,结果为int * 2.操作数中优先级最高的为int,结果为int * 3.操作数中类型优先级大于int的 ,结果类型为操作数中最高的类型 * */ /* * System.out.println((String)(c+d));//报错 cast 'int' to 'java.lang.String' * 可以前面加String 根据提示来看是什么类型。 * */ 代码演示

public class Demo05 { public static void main(String[] args) { byte b = 8; short s = 10; char c = '2'; int i = 23; long l = 211321312312L; float f = 21.3f; double d = 21.5; //变量类型不同进行运算 System.out.println((b + s));//byte + short = int System.out.println((b + c));//byte + char = int System.out.println((b + i));//byte + int = int System.out.println((b + l));//byte + long =long System.out.println((b + f));//byte + float = float System.out.println((b + d));//byte + double = double } }


1. 算术运算符

+ - * / % ++ -- 2. 赋值运算符

= 扩展赋值运算符: += -= *= /= 代码演示

public class Demo07 { public static void main(String[] args) { int a = 10; int b = 20; a += b; //a = a + b; System.out.println(a); a -= b; //a = a - b; //注意此时a为30 30-20 = 10 System.out.println(a); System.out.println("--------------------------------------------"); //字符串连接符 + int c = 10; int d = 20; System.out.println("cd的和为:" + c + d); //面试题 System.out.println("ddd" + c + d);//前面有String类型 就会把后面得 c 和 d 都转化为String类型 System.out.println(c + d + "ddd");//这是后面有String类型 先c+d得出结果30 然后与后面的String类型连接 System.out.println("Hello World !"); } } 3. 关系运算符

> < >= <= == != 代码演示

public class Demo03 { public static void main(String[] args) { 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); System.out.println(a = b); //赋值 System.out.println(a != b); //返回值为布尔类型 false true //true System.out.println(c % a); //1 System.out.println("Hello World !"); } } 4. 逻辑运算符

&& || ! 与 && and 或 || or 非 ! 取反 短路运算

public class Demo05 { public static void main(String[] args){ boolean a = true; boolean b = false; System.out.println(a && b); System.out.println(a || b); System.out.println(!(a && b)); //短路运算 int c = 5; boolean d = (c < 4) && (c++ < 4); System.out.println(d); System.out.println(c); /* * c的值没有变为6 仍为5 * 是因为boolean d = (c < 4) && (c++ < 4); 判断(c <4 )为假 直接输出结果为假 没有执行 (c++ < 4) * */ System.out.println("--------------------"); int c1 = 3; boolean d1 = (c1++ < 4);// 先3<4 true 后 c1= c1+1=4 System.out.println(d1); System.out.println(c1); int c2 = 3; boolean d2 = (++c2 < 4);// 先c2自加为4 后比较 false System.out.println(d2); System.out.println(c2); System.out.println("Hello World"); } } 5. 位运算 有面试题

& | ^ ~ >> << >>>(了解!!!) 代码演示

public class Demo06 { public static void main(String[] args) { /* 位运算*** A = 0011 1100 B = 0000 1101 -------------------------------------------- A&B 0000 1100 (A与B 对应位都为1得1 有0得0) A|B 0011 1101 (A或B 对应位都为0得0 有1得1) A^B 0011 0001 (取反/异或 对应位相同得0 不同得1) ~B 1111 0010 (B对应位为1得0 为0得1 都取反) 问2 * 8 = 16 计算机怎么算最快 2 * 2 * 2 * 2 = 16 */ /* << 左移 相当于把数字*2 >> 右移 相当于把数字/2 2 << 3 0000 0010 对应十进制2 1左移三位 0001 0000 对应十进制16 */ System.out.println(2 << 3);//2*2*2*2 System.out.println("Hello World !"); } } 6. 条件运算符(三元运算符)

? : x ? y : z 当x为true 则结果为y 否则结果为z 代码演示

//三元运算符 条件运算符 ? : //必须会 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); System.out.println("Hello World !"); } }

什么是编程中常用的运算符及其功能?