JAVA基础运算符
-
算数运算符:+,-,*,/,%,++,--
//二元运算符 //Ctrl+D赋值当前行到下一行 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);
-
赋值运算符:=
-
关系运算符:>,<,>=,<=,==,!=,instanceof
//关系运算符返回的结果:正确,错误 布尔值 //if int a =10; int b = 20; int c =21; //取余,模运算 System.out.println(c%a);// 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
-
逻辑运算符:&&,||,!
//逻辑运算符 public class Demo05 { 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));//如果为真,则输出假;如果为假,则输出真 System.out.println("!(a || b):"+!(a||b)); //短路运算 int c = 5; boolean d = (c<4)&&(c++<4); System.out.println(d); System.out.println(c);
了解即可:
-
位运算符:&,|,……,~,>>,<<,>>>
//位运算 /* A = 0011 1100 B = 0000 1101 ------------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0010 ~B = 1111 0010 2*8 = 16 2*2*2*2 在很多底层运算中使用,直接与2进制相关,效率极高!!! << *2 >> /2 */
-
条件运算符:?,:
//三元运算符 // x ? y : z //如果x为真,则输出y,否则输出z int score = 50; String type = score<60 ?"不及格":"及格"; //if System.out.println(type);
-
扩展赋值运算符:+=,-=,*=,/=
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+"");