三元运算符(条件运算符)
x ? y : z (由于是三个因素,俗称三元)
如果x ==turn ,结果为y,否则为z
条件?turn输出:false输出(相当于if语句)
package operator;
public class demo06Day07 {
public static void main(String[] args) {
int a = 59;
//boolean b=true;
String c= a>60?"及格":"不及格"; //如果a大于60,则输出第一个值,否则输出第二个值。
System.out.println(c);
}
}
扩展赋值运算符
- +=
- -+
- *=
- /=
package operator;
public class demo05Day07 {
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
//System.out.println(a);
//a*=b;// a= a*b
//System.out.println(a);
//a/=b;// a= a/b
//System.out.println(a);
}
}
字符串连接符
package operator;
public class demo07Day07 {
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+"输出结果是:");//运算后再跟字符串进行连接
/*
如果按顺序先识别到字符串,就按字符串连接不运算
*/
}
}
符号优先级,注意点
- 如果先识别字符串,就是按字符串把所有的串起来
- 如果先识别到运算,就先运算再串起来。
总结:就是看哪个优先,就执行哪个。
优先级:
- ()小括号先行运算
-
- /
-
-
- 按顺序