标签:demo 算术 stat operator 案例 运算 tor dem 算术运算符
优先级最高(),
案例如下
package operator;
public class Demo1 {
public static void main(String[] args) {
//二元运算符
int a = 10;
int b = 20;
int c = 25;
int d = 10;
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 Demo2 {
public static void main(String[] args) {
long a = 12132131231L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a + b + c + d); // long
System.out.println(b + c + d); // int
System.out.println(c + d); //int
}
}
package operator;
public class Demo3 {
public static void main(String[] args) {
//关系运算符返回的结果:true ,false
int a = 10;
int b = 12;
int c = 21;
// 取余也叫模运算
System.out.println(c % a); // 21/10=2...1
System.out.println(a > b); // false
System.out.println(a < b); //true
System.out.println(a == b); // false
System.out.println(a != b); // true
}
}
package operator;
public class Demo4 {
public static void main(String[] args) {
// ++ -- 自增 自减
// 一元运算符
int a = 3;
int b = a++; // 先赋值,后自增
// a++ a = a+1
// ++a a =a+1
int c = ++a; // 先自增,再赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(a);
// 幂运算 2^3 2*2*2=8
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
package operator;
//逻辑运算符
public class Demo5 {
public static void main(String[] args) {
// 与(and) 或(or) 非(取反)
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(d);
System.out.println(c);
}
}
package operator;
public class Demo6 {
public static void main(String[] args) {
/*
* A = 0011 1100
* B = 0000 1101
*
*
*
* A&B = 0000 1100
* A|B = 0011 1101
* A^B = 0011 0001
* ~B = 1111 0010
*
*
* 2*8 = 16 2*2*2*2
* 效率高
* << *2
* >> /2
*
*
* 0000 0000 0
* 0000 0001 1
* 0000 0010 2
* 0000 0011 3
* 0000 0100 4
* 0000 1000 8
* 0001 0000 16
* */
System.out.println(2 << 3);
}
}
package operator;
public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; // a = a+b
a -= b; // a = a-b
System.out.println(a);
//字符连接符 +, String
System.out.println(a + b);
System.out.println("" + a + b); // 如果前面是字符串就会把后面运算类型转换为String类型
System.out.println(a + b + ""); //如果在后面则不理会
}
}
package operator;
public class Demo8 {
public static void main(String[] args) {
// x ? y : z
//如果x==true ,结果则为y,否则为z
int score = 80;
String s = score < 60 ? "不及格" : "及格";
System.out.println(s);
}
}
标签:demo 算术 stat operator 案例 运算 tor dem 算术运算符
原文地址:https://www.cnblogs.com/huangwenpeng/p/14455345.html