Java基础学习:运算符3
-
条件运算符:
-
偷懒用的;
-
? :
-
-
扩展运算符:
面试题1:
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);//30
System.out.println(b);//20
?
//字符串连接符: + , string
//只要操作数两边有一个的string,结果就为string拼接
System.out.println(""+a+b);//1020
?
?
//面试题:下面的输出结果有什么区别:
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30 先运算,再拼接
}
}
三元运算符:
public class Demo08 {
public static void main(String[] args) {
//三元运算符 : x ? y: z
//如果x==true,则结果为y,否则结果为z
?
int score=80;
?
String str=score<60?"不及格":"及格";//必须掌握
System.out.println(str);
?
}
}
-
优先级:
-
优先使用:()
-
-