知识库 知识库
首页
  • 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
        • List of relational operators
        • Comparing integer numbers
        • Joining relational operations using logical operators
        • An example of a program
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
      • 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:Relational operators

  • # List of relational operators

    Java provides six relational operators to compare numbers:

    • == (equal to)
    • != (not equal to)
    • > (greater than)
    • >= (greater than or equal to)
    • < (less than)
    • <= (less than or equal to)

    The result of applying a relational operator to its operands will be boolean (true or false) regardless of the types of operands.

    # Comparing integer numbers

    Relational operators allow you to easily compare, among other things, two integer numbers. Here are some examples below:

    int one = 1;
    int two = 2;
    int three = 3;
    int four = 4;
    
    boolean oneIsOne = one == one; // true
    
    boolean res1 = two <= three; // true
    boolean res2 = two != four;  // true
    boolean res3 = two > four;   // false
    boolean res4 = one == three; // false
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    Relational operators can be used in mixed expressions together with arithmetic operators. In such expressions, relational operators have lesser priorities than arithmetic operators.

    In the following example, first of all, two sums are calculated, and then they are compared using the operator >.

    int number = 1000;
    boolean result = number + 10 > number + 9; // 1010 > 1009 is true
    
    1
    2

    The result is true.

    # Joining relational operations using logical operators

    In Java, you cannot write an expression like a <= b <= c. Instead, you should join two boolean expressions using logical operators like || and &&.

    Here is an example:

    number > 100 && number < 200; // it means 100 < number < 200 
    
    1

    Also, we can write parts of the expression in parentheses to improve readability:

    (number > 100) && (number < 200);
    
    1

    But parentheses are not necessary here because relational operators have a higher priority than logical operators.

    Here is a more general example of variables.

    int number = ...             // it has a value
    int low = 100, high = 200;   // borders
    
    boolean inRange = number > low && number < high;  // joining two expressions using AND.
    
    1
    2
    3
    4

    The code checks if the value of number belongs to a range.

    So, logical operators allow you to join a sequence of relational operations into one expression.

    # An example of a program

    Suppose there are three children in the sports class. You want to check if their heights are arranged in descending order. The following program reads three integer numbers h1, h2, and h3 and then checks if h1 >= h2 and h2 >= h3. Note that h means the height of a child.

    import java.util.Scanner;
    
    public class CheckDescOrder {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int h1 = scanner.nextInt();
            int h2 = scanner.nextInt();
            int h3 = scanner.nextInt();
    
            boolean descOrdered = (h1 >= h2) && (h2 >= h3);
    
            System.out.println(descOrdered);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    Here are several input-output pairs:

    Input 1

    185 178 172
    
    1

    Output 1

    true
    
    1

    Input 2

    181 184 177
    
    1

    Output 2

    false
    
    1

    It is possible not to use an additional variable to store the boolean result before output:

    System.out.println((h1 >= h2) && (h2 >= h3));
    
    1

    But when your condition is quite long, it is hard to understand what the code does without some explanations. A variable with a good name provides such an explanation.

编辑 (opens new window)
#Java basic
上次更新: 2022/09/26, 16:55:15
Theory:Increment and decrement
Theory:Ternary operator

← Theory:Increment and decrement Theory:Ternary operator→

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