Java随堂笔记三

今天学习了Java运算符的最后一部分

public static void main(String[] args) {
       //算数运算符:+ - * / % ++ --
       //赋值运算符:=
       //关系运算符:> < >= <= == != instanceof
       //逻辑运算符:&& || !
       //位运算:& | ^ ~ >> << >>>
       //条件运算符:? :
       //扩展赋值运算符:+= -= *= /=
       //long int short和byte互相加减时有long结果为long,否则为int
       //不同基础类型的值相加,结果选高位,最低位为int
       //关系运算符返回的结果为:布尔值
       //++ -- 自增 自减 一元运算符
       int a = 1;
       int b = a++;//赋完值再自增
       System.out.println(a);//2
       int c = ++a;//先自增再赋值
       System.out.println(a);//3
       System.out.println(b);//1
       System.out.println(c);//3

       //幂运算   java不是2^3,java里很多运算会使用一些工具类
       double pow = Math.pow(2,3);
       System.out.println(pow);

       //逻辑运算符
       //与(and)   或(or) 非(取反)
       boolean x = true;
       boolean y = false;
       System.out.println("x&&y:" + (x&&y));//false
       System.out.println("x||y:" + (x||y));//true
       System.out.println("!(x&&y):" + !(x&&y));//true

       //短路运算
       int m = 5;
       boolean n = (m < 4)&&(m++ < 5);
       System.out.println(m);//5
       System.out.println(n);//false

       //位运算
       /*
       A = 0011 1100
       B = 0000 1101
       -------------------------------
       A&B = 0000 1100 //与运算
       A|B = 0011 1101 //或运算
       A^B = 0011 0001 //亦或运算:相同为0,不相同为1
       ~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);//16

       //条件运算符三元运算符 ? :
       /*
       x ? y : z
       如果x为true,则结果为y,否则结果为z
        */
       int score = 80;
       String type = score < 60? "不及格":"及格";
       System.out.println(type);//及格

       //扩展赋值运算符
       int p = 10;
       int q = 20;
       p+=q;//p = p + q
       System.out.println(p);

       //字符串连接符   +
       System.out.println("" + a + b);//"1020"
       System.out.println(a + b + "");//"30"

 

猜你喜欢

转载自www.cnblogs.com/coffeexj/p/12023266.html