前言
本文章主要集中讲解运算符,篇幅较长,请耐心看完绝对通俗易懂。
一、算数运算符
1.简介
再Java中,使用算术运算符 +、-、*、/、%分别代表加减乘除,取模。
2.运用
+
在java中+有三种:1、正常的运算。2、作为正负来用。3、作为连接符(任意数据类型的数据与字符串相连接的时候,那个+号就是连接符)
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com; public class liu { public static void main(String[] args) { //实例一:作为正常运算 int c = 1 ; int d = 2 ; int e = c + d; System.out.println(e) //最后结果为3; //实例二:作为连接符 //已知有两个变量a和b,值分别为1和2,在控制台上输出a + b = 3, 1 + 2 = 3 int a = 1 ; int b = 2 ; System.out.println(a + " + " + b + " = " + (a + b)); //控制台显示结果为:a + b = 3。 //注意此时的"+","="作为字符串,而(a + b)则进行运算。 } } |
-
在java中+有两种:1、正常的运算。2、作为正负来用。
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class liu { public static void main(String[] args) { int a = 1 ; int b = 3 ; int c = b-a; System.out.println(c); //运算结果:2。 } } |
*
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class liu { public static void main(String[] args) { int i = 4 ; int j = 2 ; int a = i*j; System.out.println(a); //8 } } |
/
当参与/运算的两个数都是整数的话,结果为整数,反之为浮点数。
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com; public class liu { public static void main(String[] args) { //实例1:作为整数运算 int i = 4 ; int j = 2 ; int a = i / j; System.out.println(a); //2 //实例2:作为浮点运算 int i = 5 ; double j = 2 ; double a = i / j; System.out.println(a); //2.5 } } |
%
整数的取余操作(有时称为取模)用%表示。
|
1 2 3 4 5 6 7 8 9 10 11 |
package com; public class liu { public static void main(String[] args) { int i = 2 ; int j = 3 ; System.out.println(i % j); //0 int a = 4 ; int b = 2 ; System.out.println(a % b); //2 } } |
二、自增自减运算符
++
单独使用:不管++在前还是在后,结果都是一样,详见实例1。
参与运算:++在前:先自身加1,然后再参与运算,详见实例2;++在后:先参与运算,然后再自身加1,详见实例3。
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com; public class liu { public static void main(String[] args) { //实例1:单独运算。 int i = 1 ; i++; // i = i + 1 ++i; System.out.println(i); //结果都等于2。 //实例2:++在前。 int i = 1 ; int j = ++i; System.out.println(j); //2 System.out.println(i); //2 //实例3:++在后。 int i = 1 ; int j = i++; System.out.println(j); //1 System.out.println(i); //2 } } |
–
单独使用:不管–在前还是在后,结果都是一样,详见实例1。
参与运算:–在前:先自身减1,然后再参与运算,详见实例2;–在后:先参与运算,然后再自身减1,详见实例3。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com; public class liu { public static void main(String[] args) { //实例1:单独运算。 int i = 2 ; i--; // i = i - 1 --i; System.out.println(i); //结果都等于1。 //实例2:--在前。 int i = 1 ; int j = --i; System.out.println(j); //1 System.out.println(i); //1 //实例3:--在后。 int i = 1 ; int j = i--; System.out.println(j); //2 System.out.println(i); //1 } } |
三、赋值运算符
可以在赋值中使用二元运算符,形式非常简便:x += 4 等价于 x = x + 4。
常见的赋值运算符有:=、+=、-=、*=、/=、%=。
下面以+=为例:
|
1 2 3 4 5 6 7 8 9 10 11 |
package com; public class liu { public static void main(String[] args) { int i = 1 ; i += 2 ; // i = i + 2 System.out.println(i); //输出结果3 byte b = 1 ; b += 2 ; // b = (byte)(b + 2) System.out.println(b); } } |
四、关系运算符
关系运算符得出来的结果一定是boolean类型的数据,也就是说要么是true,要么是false
常见的关系运算符:>、<、>=、<=、!=、==。
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com; public class liu { public static void main(String[] args) { int i = 3 ; int j = 2 ; System.out.println(i > j); //true System.out.println(i < j); //false System.out.println(i >= j); //true System.out.println(i <= j); //false System.out.println(i != j); //true System.out.println(i == j); //false // ==是比较两边的大小,=是赋值。 } } |
五、逻辑运算符
左右两边的数据的数据类型都是boolean类型,结果都是boolean类型。
&(单与)
两边只要有一个是false,结果就为false
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println( true & true ); System.out.println( true & false ); //false System.out.println( false & false ); System.out.println( false & true ); //false } } |
| (单或)
两边只要有一个是true,结果就为true。
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println( true | true ); System.out.println( true | false ); System.out.println( false | false ); //false System.out.println( false | true ); } } |
^(异或)
两边相同为false,两边不同为true。
实例:
|
1 2 3 4 5 6 7 8 9 10 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println( true ^ true ); //false System.out.println( true ^ false ); System.out.println( false ^ false ); //false System.out.println( false ^ true );
} } |
!(非)
直接就是取反。
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println(! true ); //false System.out.println(! false ); //true System.out.println(!! true ); //true System.out.println(!!! true ); //false } } |
&&(双与)
两边只要有一个是false,结果就为false。
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println( true && true ); System.out.println( true && false ); //false System.out.println( false && false ); //false System.out.println( false && true ); //false } } |
&和&&的区别?
&&:如果左边为false,右边就不再执行了,结果就为false。 &:如果左边为false,右边依然会执行,结果就为false。
|| (双或)
两边只要有一个是true,结果就为true。
实例:
|
1 2 3 4 5 6 7 8 9 |
package com; public class Demo12 { public static void main(String[] args) { System.out.println( true || true ); //true System.out.println( true || false ); //true System.out.println( false || false ); //false System.out.println( false || true ); //true } } |
|和||的区别?
||:如果左边为true,右边就不再执行了,结果就为true。 |:如果左边为true,右边依然会执行,结果就为true。
六、位运算符
两边的数据都是数字,结果也为数字。
常见的位运算符:&(与位运算)、|(或位运算)、^(异或位运算)、~(按位取反)、>>(右移位运算)、<<(左移位运算)、>>>(无符号右移)。
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com; public class Demo01 { public static void main(String[] args) { System.out.println( 3 & 2 ); //2 System.out.println( 3 | 2 ); //3 System.out.println( 3 ^ 2 ); //1 System.out.println(~ 3 ); //-4 System.out.println( 3 >> 2 ); //0 System.out.println( 3 << 2 ); //12 System.out.println( 3 >>> 2 ); //0 System.out.println(- 3 >> 2 ); //-1 System.out.println(- 3 >>> 2 ); } } |
>>和>>>区别是什么?
>>:如果数据是负数,最左边的符号位是1,右移之后,左边要补1。
如果数据是正数,最左边的符号位是0,右移之后,左边要补0。
>>>:不管数据是正数还是负数,右移之后,左边都补0。
七、三元运算符
格式:条件表达式 ? 表达式1 : 表达式2; 等同于 x > y ? x : y
条件表达式的结果一定是boolean类型
执行流程:
如果条件表达式为true,就会执行表达式1,不会执行表达式2
如果条件表达式为false,就会执行表达式2,不会执行表达式1
实例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com; public class Demo02 { public static void main(String[] args) { //获取两个数的较大值 int i = 2 ; int j = 3 ; //第一种: int max = i > j ? i : j; System.out.println(max); //3 //第二种: System.out.println(i > j ? i : j); //表达式1和表达式2既然会得到一个结果,如果传递给一个变量去接收,该变量的数据类型应该和表达式1和表达式2的结果的数据类型匹配。 //浮点型: double d = 3 > 2 ? 1 : 2 ; System.out.println(d); //输出:1.0 //字符型: char c1 = 3 > 2 ? 97 : 98 ; System.out.println(c1); //输出:a } } |
到此这篇关于Java基础知识精通各种运算符的文章就介绍到这了,更多相关Java运算符内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_43355770/article/details/123971043