JAVA运算符
运算符
算数运算符
public class hello {
public static void main(String[] args) {
long a = 10000000L;
int n = 1;
short t = 4;
byte u = 6;
System.out.println(a+n+t+u);//Long
//只要运算中有一个其运算结果为Long
System.out.println(n+t);//int
//其余都为int
System.out.println(t+n);//int
}
}
%运算符
public class hello {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.println(a%b);//得出结果为2
}
}
关系运算符
public class hello {
public static void main(String[] args) {
int a = 5;
int b = 3;
//关系运算符的结果为布尔值返回结果为true或false
//后期多和if来配合使用
System.out.println(a==b);//false
System.out.println(a<b);//false
System.out.println(a>b);//true
System.out.println(a!=b);//true
}
}
逻辑运算符
public class hello {
public static void main(String[] args) {
//与(&&) 或(||) 非(!(&&))
boolean a = true;
boolean b = false;
System.out.println("a && b:"+ (a&&b));//逻辑与运算:两个变量都为真,输出结果才为true
System.out.println("a || b:"+ (a||b));//逻辑或运算:两个1变量中,其中有一个为真,输出结果为true
System.out.println("!(a&&b):"+ !(a&&b));//如果为真则:输出为false;如果为假则:输出为true
}
}
字符串连接符
public class hello {
public static void main(String[] args) {
int a = 10;
int n = 20;
//当表达式中有String字符串类型时,会统一转换为字符串类型进行运算
System.out.println(a+n+"");
//此操作必须String在前面生效
System.out.println(""+a+n);
}
}
控制台输出结果
30 1020
三元运算符
public class hello {
//三元运算符
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score <60 ?"不及格":"不及格";
System.out.println(type);
}
}

浙公网安备 33010602011771号