Java语言支持的运算符
-
算术运算符:+,- ,* ,/,%,++,--
-
赋值运算符:=
-
关系运算符:>,<,>=,<=,==,!=intanceof
-
逻辑运算符:&&,||,!
-
位运算符:&,|,^,~,>>,<<,>>> (了解)
-
条件运算符? :
-
扩展运算符:+=,-=,*=,/=
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);//强转doulbe类型,不然结果是0
}
}
基础算术运算符
package operator;
public class Demo02 {
public static void main(String[] args) {
long a=1089898977987L;
int b=28686860;
short c=9898;
byte d=6;
System.out.println(a+b+c); //结果为long类型
System.out.println(c+d);//结果为int
System.out.println(b+c+d);//结果为int
}
}
算术运算符扩展数据类型
package operator;
public class Demo03 {
public static void main(String[] args) {
int a=15;
int b=25;
int c=32;
int d=98;
//关系运算符的值是布尔值,只有错误和正确;起判断作用的
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println("=======================");
//取余,模运算
System.out.println(c%a);
System.out.println(d%a);
System.out.println(d%b);
System.out.println(d%c);
System.out.println("=======================");
//一元运算符,自增运算符
int a1=4;
int b1=a1++;//先b1=a1,a1=a1+1
int c1=++a1;//先a1=a1+1,再c1=a1
System.out.println(a1);
System.out.println(b1);
System.out.println(c1);
System.out.println("=======================");
//自减运算符
int d1=a1--;//先d1=a1,a1=a1-1
int f1=--a1;//先a1=a1-1,再f1=a1
System.out.println(d1);
System.out.println(f1);
System.out.println("=======================");
//幂运算,2^3 使用工具类来操作
double pow=Math.pow(4,3);//数据类型必须是double,int类型不可以
System.out.println(pow);
}
}
关系运算符,模运算,自增自减以及幂运算
package operator;
public class Demo04 {
public static void main(String[] args) {
//条件运算符,三元运算符 X?Y:Z,如果X==true,则结果为Y,否则为Z
int a=80;
int b=60;
int c=50;
String score=a>b?"分数合格":"分数不合格";//条件a>b,结果为真,则取值"分数合格"
String score2=c>b?"分数合格":"分数不合格";//条件c>b,结果为假,则取值"分数不合格"
System.out.println(score);
System.out.println(score2);
System.out.println("===============================");
b-=a;//b=b-a;
c+=a;//c=c+a;
System.out.println(b);
System.out.println(c);
//字符串连接符 + ,String
System.out.println(""+b+c);//字符串在前面,后面会拼接
System.out.println(b+c+"");//字符串在后面,前面会自动运算
}
}
条件运算符,扩展字符串连接符
标签:a1,Java,语言,int,System,运算符,println,out From: https://www.cnblogs.com/Fisherstudy/p/16924793.html