- Forward


Relational Operators
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Definitions:
    • An operator is a symbol indicating that an operation is to be performed on one or more operands
    • An operand can be a variable, literal, or expression
  • Number of Operands:
    • A unary operator has one operand
    • A binary operator has two operands
  • Values:
    • Boolean values can be either true or false
    • Integer and floating-point numbers are both considered numeric values
Going Further: Operator Notations
Back SMYC Forward
  • Infix Notation for Binary Operators:
    • Operand Operator Operand
  • Postfix Notation for Binary Operators:
    • Operand Operand Operator
  • Prefix Notation for Binary Operators:
    • Operator Operand Operand
Going Further: Operator Notations (cont.)
Back SMYC Forward
  • Other Names:
    • Prefix Notation is sometimes called Polish Notation
    • Postfix Notation is sometimes called Reverse Polish Notation
  • Resulting Humor(?):
    • /imgs
      (Courtesy of xkcd)
Relational Operators
Back SMYC Forward
  • Operands:
    • Numbers (or types that can be converted to numbers)
  • Result:
    • A Boolean
Relational Operators in Java
Back SMYC Forward
  • The Big Four:
    • Greater than: >
    • Less than: <
    • Equal to: ==
    • Not equal to: !=
  • Two More:
    • Greater than or equal to: >=
    • Less than or equal to: <=
Examples of Expressions Involving Relational Operators in Java
Back SMYC Forward
  • Involving Literals:
    • 7 < 5
    • 3 == 3
    • 7 != 7
  • Involving Variables (Declared Elsewhere):
    • this_variable < that_variable
    • this_variable >= 15
    • (x + y) != (a * b)
Operands of Relational Operators in Java
Back SMYC Forward
  • Use as Expected:
    • byte
    • short
    • int
    • long
  • Use with Care (Because of Rounding Issues):
    • float
    • double
  • Don't Use:
    • Reference types (e.g., String)
Relational Operators with boolean Operands
Back SMYC Forward
  • An Observation:
    • The == and != operators can be used with boolean operands
  • However:
    • Don't - doing so is inelegant
Relational Operators with boolean Operands (cont.)
Back SMYC Forward
  • The Inelegant Expressions:
    • x == true
    • y == false
  • Why They Are Inelegant:
    • x == true and x evaluate to exactly the same thing
      (i.e., when x evaluates to true it follows that x == true evaluates to true, and when x evaluates to false it follows that x == true evaluates to false)
    • y == false and !y evaluate to exactly the same thing
      (i.e., when y evaluates to true it follows that !y evaluates to false and y == true evaluates to false, and when y evaluates to false it follows that !y evaluates to true and y == false evaluates to true)
  • An Analogy:
    • Using them is like using a + 0 instead of a and almost all style guides prohibit their use
Combining Relational and Logical Operators
Back SMYC Forward
  • An Observation:
    • Since relational operators evaluate to a boolean value they can be "combined" using logical operators
  • An Example:
    • (gpa >= 2.0) && (credits >=120)
Gaining Experience Combining Relational and Logical Operators
Back SMYC Forward

Which of the following expressions are syntactically correct?

a > 0 && < 100 10 < 63 < 105 10 < 63 && 10 < 105
Gaining Experience Combining Relational and Logical Operators (cont.)
Back SMYC Forward

Which of the following assignment statements are syntactically correct?

boolean a, b; int i, j; a = i < j; b = a && b; i = i + j; i = i < j; b = a && ((i == j) || (i > j));
There's Always More to Learn
Back -