逻辑运算符



                                        逻辑运算符

运算符 运算 范例 结果
& AND(与) false&true false

|

OR(或)

false|true true
^ XOR(异或) true^false true
! Not(非) !true false
&& AND(短路) false&&ture false
|| OR(短路) false||true true

&:(与)

数学(2<x<5)                    

java中应该这样写:x>2  x<5

     x>2     x<5这两个表达式是没有关系的,如果想限定x>2   x<5之间,

想这两个表达式有关系,这个关系指的就是逻辑运算符。

注:x介于2到5之间,只是两个表达式。

    逻辑运算符有什么用?

用于连接两个boolean类型的表达式。

例:x>2 & x<5

注:这个就是boolean表达式。为什么这两个是boolean式?

因为他们的结果不是ture就是false。

例:

class Demo3

{

    public static void main(String[] args)

    {

             int  x  = 3;

                 System.out.println(x>2&x<5);//结果是true.

     }

}

&(与):符号的运算特点:

true&true=true

true&false=false

false&true=false

false&false=false

&(与):运算规律:

&(与):运算的两边只要有一个是false,结果肯定是false。

只有两边是true,结果才是true。


|(或):运算特点:

true|true=true

true|false=true

false|true=true

false|false=false


|(或):运算规律

|(或)运算的两边只要有一个是true,结果肯定是true。

只有两边都为false,结果才是false。


class  Demo4

{

    public static void main(String[] srgs)

    {

         int   x  =  3;

                System.out.println(x<2|x<5);//结果是true。

     }

}

注:|(或)或的概念就是满足一个条件就可以了。



^(异或):异或和或有点不一样。

^(异或):异或的运算特点

true^true=false

true^false=true

false^true=true

false^false=false


^(异或):异或的运算规律

^(异或)异或符号的两边结果如果相同,结果是false。

两边的结果不同,结果是true。



!(非)非运算,判断事物的另一面。

!true=false

!false=true


注:!!true=true

进行两次判定,所以是true。这个一般不用,作为了解就行了。



&&(短路:双与的意思)

&&(双与):运算特点

和&(与)运算结果是一样的,但是运算过程有点小区别。


例:1

class Demo 5    

{

    public static void main(String[] args)

    {

        int  x= 1;

                System.out.println(x>2&x<5);//结果是false。

     }


}


例:2

class  Demo6

{    

    public static void main(String[] args)

    {                                

        int  x = 1;

                System.out.println(x>2&&x<5);//结果是false。


    }


}


||(短路:双或的意思)

||(双或):运算特点:

和|运算结果是一样的,但是运算过程有点小区别。


||(双或)和|(或)的区别:

|(或):无论左边的运算结果是什么,右边都参与运算。

||(双或):当左边为true时,右边不参与运算。


                                        逻辑运算符

运算符 运算 范例 结果
& AND(与) false&true false

|

OR(或)

false|true true
^ XOR(异或) true^false true
! Not(非) !true false
&& AND(短路) false&&ture false
|| OR(短路) false||true true

&:(与)

数学(2<x<5)                    

java中应该这样写:x>2  x<5

     x>2     x<5这两个表达式是没有关系的,如果想限定x>2   x<5之间,

想这两个表达式有关系,这个关系指的就是逻辑运算符。

注:x介于2到5之间,只是两个表达式。

    逻辑运算符有什么用?

用于连接两个boolean类型的表达式。

例:x>2 & x<5

注:这个就是boolean表达式。为什么这两个是boolean式?

因为他们的结果不是ture就是false。

例:

class Demo3

{

    public static void main(String[] args)

    {

             int  x  = 3;

                 System.out.println(x>2&x<5);//结果是true.

     }

}

&(与):符号的运算特点:

true&true=true

true&false=false

false&true=false

false&false=false

&(与):运算规律:

&(与):运算的两边只要有一个是false,结果肯定是false。

只有两边是true,结果才是true。


|(或):运算特点:

true|true=true

true|false=true

false|true=true

false|false=false


|(或):运算规律

|(或)运算的两边只要有一个是true,结果肯定是true。

只有两边都为false,结果才是false。


class  Demo4

{

    public static void main(String[] srgs)

    {

         int   x  =  3;

                System.out.println(x<2|x<5);//结果是true。

     }

}

注:|(或)或的概念就是满足一个条件就可以了。



^(异或):异或和或有点不一样。

^(异或):异或的运算特点

true^true=false

true^false=true

false^true=true

false^false=false


^(异或):异或的运算规律

^(异或)异或符号的两边结果如果相同,结果是false。

两边的结果不同,结果是true。



!(非)非运算,判断事物的另一面。

!true=false

!false=true


注:!!true=true

进行两次判定,所以是true。这个一般不用,作为了解就行了。



&&(短路:双与的意思)

&&(双与):运算特点

和&(与)运算结果是一样的,但是运算过程有点小区别。


例:1

class Demo 5    

{

    public static void main(String[] args)

    {

        int  x= 1;

                System.out.println(x>2&x<5);//结果是false。

     }


}


例:2

class  Demo6

{    

    public static void main(String[] args)

    {                                

        int  x = 1;

                System.out.println(x>2&&x<5);//结果是false。


    }


}


||(短路:双或的意思)

||(双或):运算特点:

和|运算结果是一样的,但是运算过程有点小区别。


||(双或)和|(或)的区别:

|(或):无论左边的运算结果是什么,右边都参与运算。

||(双或):当左边为true时,右边不参与运算。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值