算术,赋值,关系,运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=30;
int d=40;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a=1000_0000L;
int b=123;
short c=10;
byte d=8;
double e=3.1;
System.out.println(a+b+c+d); //long
System.out.println(b+c+d); //int
System.out.println(c+d); //int
System.out.println(c+d+e); //double
System.out.println(a+e); //double
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
int a=10;
int b=25;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
//关系运算符返回的结果:布尔值 true false
System.out.println(b%a); //返回结果5,取余数意思,又叫模运算
}
}
自增自减,一元运算符
public class Demo04 {
public static void main(String[] args) {
int a=10;
int b=a++; //++在a后面,所以a先给b赋值,然后再自增
System.out.println(a); //11
System.out.println(b); //10
int c=10;
int d=++c; //++在c前面,所以c先自增,然后再给d赋值
System.out.println(c); //11
System.out.println(d); //11
}
}
逻辑运算符
&&与
||或
!非
public class Demo06 {
public static void main(String[] args) {
boolean a=true;
boolean b=false;
System.out.println(a&&b); //两个变量都为真,结果才为true
System.out.println(a||b); //两个变量有一个为真,结果就是true
System.out.println(!(a&&b));//如果是真,则变为假
//短路运算
int c=5;
boolean d=(c<4)&&(c++<4); //&&前为false,后面不再计算
System.out.println(c); //5
System.out.println(d); //false
}
}
位运算(了解)
字符串连接符
public class Demo07 {
public static void main(String[] args) {
int a=10;
int b=20;
//字符串连接符
System.out.println("王"+a+b); //王1020,直接拼接
System.out.println(a+b+"王"); //30王,a+b运算后拼接
}
}
条件运算符?:
x ? y : z
如果x==true,则结果为y,否则结果为z
public class Demo08 {
public static void main(String[] args) {
int score=80;
String type=score<60?"不及格":"及格";
System.out.println(type);
}
}
标签:基本,String,int,System,运算符,println,public,out
From: https://www.cnblogs.com/PedroPascal/p/17093164.html