基本运算
public class Demo01 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
计算时的类型转换
package com.Chao.operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123456789L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);
System.out.println(b+c+d);
System.out.println((c+d));
}
}
关系运算
package com.Chao.operator;
public class Demo03 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a);
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
自增自减
package com.Chao.operator;
public class Demo04 {
public static void main(String[] args) {
int a = 3;
int b = a++;
System.out.println(a);
int c = ++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
double pow = Math.pow(2, 3);
System.out.println(pow);
Math.pow(2,3);
}
}
逻辑运算
package com.Chao.operator;
public class Demo05 {
public static void main(String[] args) {
boolean a = true;
boolean b =false;
System.out.println("a && b:"+(a&&b));
System.out.println("a || b:"+(a||b));
System.out.println("!(a && b):"+!(a&&b));
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(c);
System.out.println(d);
}
}
位运算
package com.Chao.operator;
public class Demo06 {
public static void main(String[] args) {
System.out.println(2<<3);
}
}
字符串连接
package com.Chao.operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;
a-=b;
System.out.println(a);
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
三元运算符
package com.Chao.operator;
public class Demo08 {
public static void main(String[] args) {
int score = 80;
String type = score<60 ?"不及格":"及格";
System.out.println(type);
}
}
|