运算符

运算符

逻辑运算符

  • &&
  • ||
  • !
public class Demo1 {
    public static void main(String[] args) {
        boolean a=true;
        boolean b=false;
        System.out.println("a&&b:"+(b&&a));
        System.out.println("a||b:"+(b||a));
        System.out.println("!a&&b:"+!(b&&a));

        int c=5;
        boolean d=(c<4)&&(c++<5);//前面已经是false 后面语句不执行
        System.out.println(d);
        System.out.println(c);
    }
}

位运算符

  • &

  • |

  • ^

  • ~

  • <<

  • _>>

    public class Demo2 {
        public static void main(String[] args) {
            /*
            A=0011 1100
            B=0000 1101
    
            与   A&B=0000 1100 都为1才为1
            或   A|B=0011 1101 都为0才为0
            异或 A^B=0011 0001 相同为0,不同为1
            取反 ~B=1111 0010 取反
    
            效率极高!!
            左移 << 相当于把数字*2^n
            右移 >> 相当于把数字/2^n
             */
            System.out.println(16>>3); //输出2
        }
    }
    
    

    二元运算符,字符串连接符

    public class Democrat {
        public static void main(String[] args) {
            int a=10;
            int b=20;
            System.out.println(""+a+b);//1020 +字符串连接符 String先出现,自动将后面转换连接
            System.out.println(a+b+"");//30    ""后出现先加再连接字符串   
            //x?y:z
            //如果x==true,则结果为y,否则为z
        }
    }
    
    
posted @ 2023-06-04 21:42  晚枫zz  阅读(8)  评论(0)    收藏  举报