a+ a-
package com.operator;
public class Dome01 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 20;
a+=b;//a=a+b
System.out.println(a);
a-=c;//a=a-c
System.out.println(a);
//字符串连接符 "" + 如果计算过程中前面先出现了string类型,那么后面的数值就会被转换为string类型被衔接起来
System.out.println(""+a+b);//输出结果1020 先出现string类型,后面代码直接执行的是文字的衔接而不是进行计算
//面试题
System.out.println(a+b+"");//输出结果30
}
}
三元运算符
package om.operator;
//三元运算符
public class Demo02 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String a =score>60?"成绩及格":"不及格";
//if也能做,但是这么做代码更精简
System.out.println(a);//输出结果成绩及格
//优先级()
int d = 10;
int b = 20;
Boolean c = d<b;
System.out.println(c);
}
}