(1)算术运算符
/和%的区别:
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">定义变量
<span style="color: #0000ff;">int x = 3; <span style="color: #008000;">//<span style="color: #008000;">把3赋值给int类型的变量x
<span style="color: #0000ff;">int y = 4<span style="color: #000000;">;
System.out.println(x</span>+<span style="color: #000000;">y);
System.out.println(x</span>-<span style="color: #000000;">y);
System.out.println(x</span>*<span style="color: #000000;">y);
System.out.println(x</span>/y); <span style="color: #ff0000;">//得到0,整数相除只能得到整数</span>
<span style="color: #ff0000;">//我就想得到小数,该肿么办呢?
//只需要把操作的数据中任意的一个数据变为浮点数</span>
System.out.println(x*<span style="color: #ff0000;">1.0</span>/y);
<span style="color: #ff0000;">//%的应用</span>
System.out.println(x%y); <span style="color: #008000;">//</span><span style="color: #008000;">得到的是余数</span>
<span style="color: #000000;"> }
}
++,--运算符的使用;
作用:就是对变量进行自增1或者自减1。
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorDemo2 {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">定义两个变量
<span style="color: #0000ff;">int x = 3<span style="color: #000000;">;
<span style="color: #0000ff;">int y = 4<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">字符串的拼接,字符串在前面</span>
<span style="color: #008000;">//</span><span style="color: #008000;">System.out.println("x:"+x);</span>
<span style="color: #008000;">//</span><span style="color: #008000;">System.out.println("y:"+y);</span>
<span style="color: #000000;">
System.out.println("x:"+x+",y:"+<span style="color: #000000;">y);
</span><span style="color: #008000;">//</span><span style="color: #008000;">单独使用</span>
<span style="color: #008000;">//</span><span style="color: #008000;">x++;</span>
<span style="color: #008000;">//</span><span style="color: #008000;">y--;</span>
++<span style="color: #000000;">x;
</span>--<span style="color: #000000;">y;
</span><span style="color: #008000;">//</span><span style="color: #008000;">System.out.println(x);</span>
System.out.println("x:"+x+",y:"+<span style="color: #000000;">y);
</span><span style="color: #008000;">//</span><span style="color: #008000;">意外的类型,常量是不可以这样做的</span>
<span style="color: #008000;">//</span><span style="color: #008000;">System.out.println(10++);</span>
<span style="color: #000000;">
System.out.println("-------------------"<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;">参与运算使用
<span style="color: #0000ff;">int a = 3<span style="color: #000000;">;
<span style="color: #0000ff;">int b = 4<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">int c = a++;</span>
<span style="color: #008000;">//</span><span style="color: #008000;">int d = b--;</span>
<span style="color: #ff0000;"> int c = ++a; //++放在操作数前面,先自增或者自减,然后再赋值</span>
<span style="color: #0000ff;">int</span> d = --<span style="color: #000000;">b;
System.out.println(</span>"a:"+a); <span style="color: #008000;">//</span><span style="color: #008000;">4,4</span>
System.out.println("b:"+b); <span style="color: #008000;">//</span><span style="color: #008000;">3,3</span>
System.out.println("c:"+c); <span style="color: #008000;">//</span><span style="color: #008000;">3,4</span>
System.out.println("d:"+d); <span style="color: #008000;">//</span><span style="color: #008000;">4,3</span>
<span style="color: #000000;"> }
}
运算符的优先级:
+的用法:
</span><span style="color: #008000;">//</span><span style="color: #008000;">正号</span>
System.out.println(+4<span style="color: #000000;">);
System.out.println(</span>'a');<span style="color: #008000;">//</span><span style="color: #008000;">直接<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>字符a</span>
System.out.println('a'+1); <span style="color: #008000;">//</span><span style="color: #008000;">字符a和1的加法,这里是加法<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>98</span>
<span style="color: #008000;">//</span><span style="color: #008000;">字符串连接符,注意是字符串而不是字符</span>
System.out.println("hello"+'a'+1);<span style="color: #008000;">//</span><span style="color: #008000;">helloa1,字符串hello和字符a<a href="https://www.jb51.cc/tag/lianjie/" target="_blank" class="keywords">链接</a>,得到字符串helloa,再和<a href="https://www.jb51.cc/tag/lianjie/" target="_blank" class="keywords">链接</a></span>
System.out.println('a'+1+"hello");<span style="color: #008000;">//</span><span style="color: #008000;">98hello,先进行字符a和1的加法运算,再和字符串hello<a href="https://www.jb51.cc/tag/lianjie/" target="_blank" class="keywords">链接</a></span>
<span style="color: #000000;">
}
}
(2)赋值运算符
例子:
</span><span style="color: #0000ff;">short</span> s = 1<span style="color: #000000;">;
s </span>= s + 1<span style="color: #000000;">;
</span><span style="color: #0000ff;">short</span> s = 1<span style="color: #000000;">;
s </span>+= 1<span style="color: #000000;">;
请问上面的<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>哪个有问题</span>?</pre>
赋值运算符:
扩展的赋值运算符:+=,%=
+= 把左边和右边做加法,然后赋值给左边。
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorDemo {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">定义一个变量
<span style="color: #0000ff;">int x = 10<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">其他<a href="https://www.jb51.cc/tag/yongfa/" target="_blank" class="keywords">用法</a></span>
<span style="color: #0000ff;">int</span><span style="color: #000000;"> a,b;
a </span>= b = 10;<span style="color: #008000;">//</span><span style="color: #008000;">把10赋给b,再把b赋值给a。这种写法不建议</span>
<span style="color: #000000;"> System.out.println(a);
System.out.println(b);
System.out.println("-----------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">定义一个变量</span>
<span style="color: #0000ff;">int</span> y = 10<span style="color: #000000;">;
y </span>+= 20;<span style="color: #008000;">//</span><span style="color: #008000;">y = y + 20</span>
<span style="color: #000000;">
System.out.println(y);
}
}
扩展的赋值运算符其实隐含了一个强制类型转换:
short s=1;s = s+1;
short s=1;s+=1;
上面两个<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>有没有问题,如果有,那里有问题。
<span style="color: #ff0000;">为什么第二个木有问题呢?
扩展的赋值运算符其实隐含了一个强制类型转换。</span>
<span style="color: #ff0000;">s += 1;</span>
不是等价于 s = s + 1;
<span style="color: #ff0000;">而是等价于 s = (s的数据类型)(s + 1);
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorTest {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">short s = 1;
<span style="color: #008000;">//<span style="color: #008000;">s = s + 1;
<span style="color: #008000;">//<span style="color: #008000;">System.out.println(s);
<span style="color: #ff0000;">//s = s + 1;//short类型,先转成int,和1相加,再赋值给short类型,报错
<span style="color: #0000ff;">short s = 1<span style="color: #000000;">;
s += 1; <span style="color: #008000;">//<span style="color: #008000;">好像是 s = s + 1;
<span style="color: #000000;"> System.out.println(s);
}
}
(3)比较运算符
特点:
无论你的操作是简单还是复杂,结果是boolean类型。
注意事项:
"=="不能写成"="。
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorDemo {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #0000ff;">int x = 3<span style="color: #000000;">;
<span style="color: #0000ff;">int y = 4<span style="color: #000000;">;
<span style="color: #0000ff;">int z = 3<span style="color: #000000;">;
System.out.println(x </span>==<span style="color: #000000;"> y);
System.out.println(x </span>==<span style="color: #000000;"> z);
System.out.println((x</span>+y) == (x+<span style="color: #000000;">z));
System.out.println(</span>"------------"<span style="color: #000000;">);
System.out.println(x </span>!=<span style="color: #000000;"> y);
System.out.println(x </span>><span style="color: #000000;"> y);
System.out.println(x </span>>=<span style="color: #000000;"> y);
System.out.println(x </span><<span style="color: #000000;"> y);
System.out.println(x </span><=<span style="color: #000000;"> y);
System.out.println(</span>"------------"<span style="color: #000000;">);
</span><span style="color: #0000ff;">int</span> a = 10<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> b = 20<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">boolean flag = (a == b);</span>
<span style="color: #008000;">//</span><span style="color: #008000;">boolean flag = (a = b); //这个是有问题的,不兼容的类型。</span>
<span style="color: #ff0000;"> //把b赋值给a,然后把a留下来,而a是int类型,赋值给flag的布尔类型,报错</span>
<span style="color: #008000;">//</span><span style="color: #008000;">System.out.println(flag);</span>
<span style="color: #0000ff;">int</span> c = (a = b); <span style="color: #008000;">//</span><span style="color: #008000;">把b赋值给a,然后把a留下来,而a是int类型,赋值给flag的布尔类型,出错</span>
System.out.println(c);</pre>
(4)逻辑运算符
</span>&&:结果和&<span style="color: #000000;">是一样的,只不过有短路<a href="https://www.jb51.cc/tag/xiaoguo/" target="_blank" class="keywords">效果</a>。左边是false,右边不执行。
</span>||:结果和|是一样的,只不过有短路<a href="https://www.jb51.cc/tag/xiaoguo/" target="_blank" class="keywords">效果</a>。左边是true,右边不执行。</pre>
特点:
逻辑运算符一般用于连接boolean类型的表达式或者值。
表达式:就是用运算符把常量或者变量连接起来的符合java语法的式子。
算术表达式:a + b
比较表达式:a == b
结论:
<span style="color: #ff0000;">&逻辑与:有false则false。
|逻辑或:有true则true。
^逻辑异或:相同为false,不同为true。
举例:情侣关系。男男,男女,女男,女女
!逻辑非:非false则true,非true则false。
特点:偶数个不改变本身。
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorDemo {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #0000ff;">int a = 3<span style="color: #000000;">;
<span style="color: #0000ff;">int b = 4<span style="color: #000000;">;
<span style="color: #0000ff;">int c = 5<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">&逻辑与</span>
System.out.println((a > b) & (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">false & false = false</span>
System.out.println((a > b) & (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">false & true = false</span>
System.out.println((a < b) & (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">true & false = false</span>
System.out.println((a < b) & (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">true & true = true</span>
System.out.println("---------------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">|逻辑或</span>
System.out.println((a > b) | (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">false | false = false</span>
System.out.println((a > b) | (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">false | true = true</span>
System.out.println((a < b) | (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">true | false = true</span>
System.out.println((a < b) | (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">true | true = true</span>
System.out.println("---------------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">^逻辑异或</span>
System.out.println((a > b) ^ (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">false ^ false = false</span>
System.out.println((a > b) ^ (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">false ^ true = true</span>
System.out.println((a < b) ^ (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">true ^ false = true</span>
System.out.println((a < b) ^ (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">true ^ true = false</span>
System.out.println("---------------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">!逻辑非</span>
System.out.println(!(a > b)); <span style="color: #008000;">//</span><span style="color: #008000;">!false = true</span>
System.out.println(!(a < b)); <span style="color: #008000;">//</span><span style="color: #008000;">!true = false</span>
System.out.println(!!(a > b)); <span style="color: #008000;">//</span><span style="color: #008000;">!!false = false</span>
System.out.println(!!!(a > b)); <span style="color: #008000;">//</span><span style="color: #008000;">!!false = true</span>
<span style="color: #000000;"> }
}
&&和&的区别? 同理||和|的区别?
开发中常用的逻辑运算符:
&&,||,!
<span style="color: #008000;">*/<span style="color: #000000;">class OperatorDemo2 {
public static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #0000ff;">int a = 3<span style="color: #000000;">;
<span style="color: #0000ff;">int b = 4<span style="color: #000000;">;
<span style="color: #0000ff;">int c = 5<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">&&双与</span>
System.out.println((a > b) && (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">false && false = false</span>
System.out.println((a > b) && (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">false && true = false</span>
System.out.println((a < b) && (a > c)); <span style="color: #008000;">//</span><span style="color: #008000;">true && false = false</span>
System.out.println((a < b) && (a < c)); <span style="color: #008000;">//</span><span style="color: #008000;">true && true = true</span>
System.out.println("----------------"<span style="color: #000000;">);
</span><span style="color: #0000ff;">int</span> x = 3<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> y = 4<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">boolean b1 = ((x++ == 3) & (y++ == 4));</span>
<span style="color: #008000;">//</span><span style="color: #008000;">boolean b1 = ((x++ == 3) && (y++ == 4));</span>
<span style="color: #008000;">//</span><span style="color: #008000;">boolean b1 = ((++x == 3) & (y++ == 4));</span>
<span style="color: #0000ff;">boolean</span> b1 = ((++x == 3) && (y++ == 4<span style="color: #000000;">));
System.out.println(</span>"x:"+<span style="color: #000000;">x);
System.out.println(</span>"y:"+<span style="color: #000000;">y);
System.out.println(b1);
}
}
位运算
位运算符:&,~ 解释
注意:
要做位运算,首先要把数据转换为二进制。
<span style="color: #008000;">*/<span style="color: #0000ff;">class<span style="color: #000000;"> OperatorDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">&,~
<span style="color: #0000ff;">int</span> a = 3<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> b = 4<span style="color: #000000;">;
System.out.println(</span>3 & 4);<span style="color: #008000;">//</span><span style="color: #008000;">当&两边是数据,做的是位运算;当&两边是布尔值,做的是逻辑运算</span>
System.out.println(3 | 4<span style="color: #000000;">);
System.out.println(</span>3 ^ 4<span style="color: #000000;">);
System.out.println(</span>~3<span style="color: #000000;">);
}
}
<span style="color: #008000;">/*<span style="color: #008000;">
分析:因为是位运算,所以我们必须先把数据换算成二进制。
3的二进制:11
00000000 00000000 00000000 00000011
4的二进制:100
00000000 00000000 00000000 00000100
<span style="color: #ff0000;">&位与运算:有0则0。</span>
00000000 00000000 00000000 00000011
&00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000000
结果是:0
<span style="color: #ff0000;">|位或运算:有1则1。</span>
00000000 00000000 00000000 00000011
|00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000111
结果是:7
<span style="color: #ff0000;">^位异或运算:相同则0,不同则1。</span>
00000000 00000000 00000000 00000011
&00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000111
结果是:7
<span style="color: #ff0000;">~按位取反运算符:0变1,1变0,针对补码</span>
在计算机内,有符号数有3种表示法:原码、反码和补码。所有数据的运算都是采用补码进行的。
原码:就是二进制定点表示法,即最高位为符号位,“0”表示正,“1”表示负,其余位表示数值的大小。
反码:
正数的反码与其原码相同;负数的反码是对其原码逐位取反,但符号位除外。
补码
正数的补码与其原码相同;负数的补码是在其反码的末位加1。
00000000 00000000 00000000 00000011 //正数的原码、反码、补码相同,这里代表3的补码
<span style="color: #ff0000;">~11111111 11111111 11111111 11111100 (补码) ~3 按位对3的补码取反得到的</span>
//下面通过补码,求得原码
补码:11111111 11111111 11111111 11111100
反码:11111111 11111111 11111111 11111011
原码:10000000 00000000 00000000 00000100
结果是:-4
<span style="color: #008000;">*/
^异或的特点:一个数据对另一个数据位异或两次,该数本身不变。
System.out.println(a </span>^ b ^ b); <span style="color: #008000;">//</span><span style="color: #008000;">10</span>
System.out.println(a ^ b ^ a); <span style="color: #008000;">//</span><span style="color: #008000;">20</span>
<span style="color: #000000;"> }
}
实现两个整数变量的交换:
<span style="color: #0000ff;">class<span style="color: #000000;"> OperatorTest {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #0000ff;">int a = 10<span style="color: #000000;">;
<span style="color: #0000ff;">int b = 20<span style="color: #000000;">;
System.out.println(</span>"a:"+a+",b:"+<span style="color: #000000;">b);
</span><span style="color: #008000;">//</span><span style="color: #008000;">方式1:使用第三方变量(开发中用的)</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">
int c = a;
a = b;
b = c;
System.out.println("a:"+a+",b:"+b);
System.out.println("------------");
</span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">方式2:用位异或实现(面试用)
</span><span style="color: #008000;">//</span><span style="color: #008000;">左边:a,a
</span><span style="color: #008000;">//</span><span style="color: #008000;">右边:a ^ b</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">
a = a ^ b;
b = a ^ b; //a ^ b ^ b = a
a = a ^ b; //a ^ b ^ a = b
System.out.println("a:"+a+",b:"+b);
</span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">方式3:用变量相加的做法</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">
a = a + b; //a=30,先把和保存
b = a - b; //b=10,
a = a - b; //a=20
System.out.println("a:"+a+",b:"+b);
</span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">方式4:一句话搞定</span>
b = (a+b) - (a=b); <span style="color: #008000;">//</span><span style="color: #008000;">b=30-20=10,a=20 </span><span style="color: #008000;">//</span><span style="color: #008000;">先把b赋值给a,然后用和减去a得到b</span>
System.out.println("a:"+a+",b:"+<span style="color: #000000;">b);
}
}
位移运算
请用最有效率的方式写出计算2乘以8的结果?
2 * 8
2 << 3
<span style="color: #008000;">/<span style="color: #0000ff;">class<span style="color: #000000;"> OperatorDemo3 {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;"><< 把<<左边的数据乘以2的移动次幂
System.out.println(3 << 2); <span style="color: #008000;">//<span style="color: #008000;">32^2 = 3*4 = 12;
</span><span style="color: #008000;">//</span><span style="color: #008000;">>> 把>>左边的数据除以2的移动次幂</span>
System.out.println(24 >> 2); <span style="color: #008000;">//</span><span style="color: #008000;">24 / 2^2 = 24 / 4 = 6</span>
<span style="color: #000000;">
System.out.println(24 >>> 2<span style="color: #000000;">);
System.out.println(</span>-24 >> 2<span style="color: #000000;">);
System.out.println(</span>-24 >>> 2<span style="color: #000000;">);
}
}
<span style="color: #008000;">/*<span style="color: #008000;">
计算出3的二进制:11
00000000 00000000 00000000 00000011
(00)000000 00000000 00000000 0000001100
>>的移动:
计算出24的二进制:11000
-24原码:10000000 00000000 00000000 00011000
反码:11111111 11111111 11111111 11100111
补码:11111111 11111111 11111111 11101000
//>>:右移 最高位是0,左边补齐0;最高为是1,左边补齐1
11111111 11111111 11111111 11101000
1111111111 11111111 11111111 111010(00) 补码
//将补码转成原码
补码:1111111111 11111111 11111111 111010
反码:1111111111 11111111 11111111 111001
原码:1000000000 00000000 00000000 000110
结果:-6
>>>的移动:
计算出24的二进制:11000
原码:10000000 00000000 00000000 00011000
反码:11111111 11111111 11111111 11100111
补码:11111111 11111111 11111111 11101000
11111111 11111111 11111111 11101000
0011111111 11111111 11111111 111010(00)//这里原、反、补都相同
结果:
<span style="color: #008000;">*/
三目运算
三目运算符:
格式:比较表达式?表达式1:表达式2;
比较表达式:结果是一个boolean类型。
执行流程:
根据比较表达式的计算返回一个true或者false。
如果是true,就把表达式1作为结果。
如果是false,就把表达式2作为结果。
<span style="color: #008000;">*/<span style="color: #0000ff;">class<span style="color: #000000;"> OperatorDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #0000ff;">int x = 100<span style="color: #000000;">;
<span style="color: #0000ff;">int y = 200<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> z = ((x > y)?<span style="color: #000000;"> x: y);
</span><span style="color: #008000;">//</span><span style="color: #008000;">int z = ((x < y)? x: y);
</span><span style="color: #008000;">//</span><span style="color: #008000;">int z = ((x == y)? x: y);
</span><span style="color: #008000;">//</span><span style="color: #008000;">报错
</span><span style="color: #008000;">//</span><span style="color: #008000;">int z = ((<span style="color: #ff0000;">x = y</span>)? x : y);</span><span style="color: #ff0000;">//表达式是布尔类型,才能判断。这里把y赋值给x,而x是int类型</span>
<span style="color: #000000;">
System.out.println("z:"+<span style="color: #000000;">z);
}
}
获取最大值,判断值是否相等
</span><span style="color: #0000ff;">int</span> max = (x > y?<span style="color: #000000;"> x: y);
System.out.println(</span>"max:"+<span style="color: #000000;">max);
System.out.println(</span>"--------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>三个整数中的最大值</span>
<span style="color: #0000ff;">int</span> a = 10<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> b = 30<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> c = 20<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">分两步:
</span><span style="color: #008000;">//</span><span style="color: #008000;">A:先比较a,b的最大值
</span><span style="color: #008000;">//</span><span style="color: #008000;">B:拿a,b的最大值在和c进行比较</span>
<span style="color: #0000ff;">int</span> temp = ((a > b)?<span style="color: #000000;"> a: b);
</span><span style="color: #008000;">//</span><span style="color: #008000;">System.out.println(temp);</span>
<span style="color: #0000ff;">int</span> max1 = (temp > c?<span style="color: #000000;"> temp: c);
System.out.println(</span>"max1:"+<span style="color: #000000;">max1);
</span><span style="color: #008000;">//</span><span style="color: #008000;">一步搞定
</span><span style="color: #008000;">//</span><span style="color: #008000;">int max2 = (a > b)?((a > c)? a: c):((b > c)? b: c);
</span><span style="color: #008000;">//</span><span style="color: #008000;">这种做法不推荐。
</span><span style="color: #008000;">//</span><span style="color: #008000;">int max2 = a > b?a > c? a: c:b > c? b: c;
</span><span style="color: #008000;">//</span><span style="color: #008000;">System.out.println("max2:"+max2);</span>
System.out.println("--------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">比较两个整数是否相同</span>
<span style="color: #0000ff;">int</span> m = 100<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> n = 200<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">boolean flag = (m == n)? true: false;</span>
<span style="color: #0000ff;">boolean</span> flag = (m ==<span style="color: #000000;"> n);
System.out.println(flag);
}
}
原文链接:https://www.f2er.com/java/403360.html