知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

      • Theory:Scanning the input
      • Theory:Arithmetic operations
      • Theory:Integer types and operations
      • Theory:Increment and decrement
      • Theory:Relational operators
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
        • Boolean type
        • Logical operators
        • The precedence of logical operators
        • An example: trekking
        • Short-circuiting evaluation
      • Theory:Sizes and ranges
      • Theory:Switch statement
      • Theory:Declaring a method
      • Theory:The main method
      • Theory:Type casting
      • Theory:Primitive and reference types
      • Theory:Array
      • Theory:Arrays as parameters
      • Theory:Iterating over arrays
      • Theory:Multidimensional array
      • Theory:Final variables
    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java basic
Jim
2022-08-09
目录

Theory:Boolean and logical operators

  • # Boolean type

    The boolean is a data type that has only two possible values: false and true. This is also known as the logical type.

    This type is a common way in programming languages to represent something that has only two opposite states like on or off, yes or no, etc.

    img

    If you are writing an application that keeps track of opening and closing a door you'll find it natural to use boolean to store current door state.

    boolean open = true;
    boolean closed = false;
    
    System.out.println(open);   // true
    System.out.println(closed); // false
    
    1
    2
    3
    4
    5

    Important, you cannot assign an integer value to a boolean variable. In Java, 0 is not the same as false.

    # Logical operators

    Variables of the boolean type are often used to build logical expressions using logical operators. Java has four logical operators NOT, AND, OR and XOR:

    • NOT is a unary operator that reverses the boolean value. It is denoted as !.
    boolean f = false; // f is false
    boolean t = !f;    // t is true
    
    1
    2
    • AND is a binary operator that returns true if both operands are true, otherwise, it returns false. It is denoted as &&.
    boolean b1 = false && false; // false
    boolean b2 = false && true;  // false
    boolean b3 = true && false;  // false
    boolean b4 = true && true;   // true 
    
    1
    2
    3
    4
    • OR is a binary operator that returns true if at least one operand is true, otherwise, it returns false. It is denoted as ||.
    boolean b1 = false || false; // false
    boolean b2 = false || true;  // true
    boolean b3 = true || false;  // true
    boolean b4 = true || true;   // true
    
    1
    2
    3
    4
    • XOR (exclusive OR) is a binary operator that returns true if boolean operands have different values, otherwise, it returns false. It is denoted as ^.
    boolean b1 = false ^ false; // false
    boolean b2 = false ^ true;  // true
    boolean b3 = true ^ false;  // true
    boolean b4 = true ^ true;   // false
    
    1
    2
    3
    4

    The XOR operator is used less often than others. Just remember that Java has it. If you really need it, you can use it.

    # The precedence of logical operators

    Below are the logical operations sorted in order of decreasing their priorities in expressions: ! (NOT), ^ (XOR), && (AND), || (OR).

    So, the following variable is true:

    boolean b = true && !false; // true, because !false is evaluated first
    
    1

    To change the order of execution you can use round brackets (...).

    # An example: trekking

    As an example, let's write a complex boolean expression that determines the possibility of trekking in summer and in other seasons. Let's suppose that the right conditions for trekking is warm dry weather in summer and autumn.

    boolean cold = false;
    boolean dry = true;
    boolean summer = false; // suppose now is autumn
    
    boolean trekking = dry && (!cold || summer); // true, let's go to trek!
    
    1
    2
    3
    4
    5

    Do not get confused in the expression above, otherwise, you will go trek in bad weather! A programmer should understand not only arithmetic but also logical operations.

    # Short-circuiting evaluation

    An interesting thing is that the && and || operators don't evaluate the second argument if it isn't necessary. When the first argument of the && operator evaluates to false, the overall value must be false; and when the first argument of the || operator evaluates to true, the overall value must be true. So:

    • false && ... -> false, since it is not necessary to know what the right-hand side is;
    • true || ... -> true, since it is not necessary to know what the right-hand side is.

    This behavior is known as short-circuit evaluation (do not confuse it with an electrical short circuit (opens new window)). It reduces the computation time, but can also be used to avoid some errors in programs. We will discuss this in the following topics.

编辑 (opens new window)
#Java basic
上次更新: 2022/09/26, 16:55:15
Theory:String
Theory:Sizes and ranges

← Theory:String Theory:Sizes and ranges→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式