CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
Control statements
Compare numeric variables

Relational operators

Operator Name Returns a true value if
== Equality Both operands are equal.
!= Inequality The left and right operands are not equal.
> Greater Than The left operand is greater than the right operand.
< Less Than The left operand is less than the right operand.
>= Greater Than Or Equal The left operand is greater than or equal to the right operand.
<= Less Than Or Equal The left operand is less than or equal to the right operand.

Examples of conditional expressions

discountPercent == 2.3    // equal to a numeric literal
subtotal != 0        	  // not equal to a numeric literal
years > 0            	  // greater than a numeric literal
i < months            	  // less than a variable
subtotal >= 500			  // greater than or equal to a numeric literal
quantity <= reorderPoint  // less than or equal to a variable
  • You can use the relational operators to compare two numeric operands and return a Boolean value that is either true or false.
  • To compare two numeric operands for equality, make sure to use two equals signs. If you only use one equals sign, you'll code an assignment statement, and your code won't compile.
  • If you compare an integer operand with a double operand, Java will cast the integer operand to a double.

Code that uses logical operators.

public class LogicalOperators
{
    public static void main(String[] args)
    {
        boolean found = true;
        boolean flag = false;
        double x = 5.2;
        double y = 3.4;
        int a = 5, b = 8;
        int n = 20;
        char ch = 'B';

        System.out.println("Line 1: !found evaluates to "
                         + !found);                       //Line 1
        System.out.println("Line 2: x > 4.0 evaluates to "
                      + (x > 4.0));                       //Line 2
        System.out.println("Line 3: !found && (x >= 0) "
                         + "evaluates to "
                         + (!found && (x >= 0)));         //Line 3
        System.out.println("Line 4: !(found && (x >= 0)) "
                         + "evaluates to "
                         + !(found && (x >= 0)));         //Line 4
        System.out.println("Line 5: x + y <= 20.5 evaluates to "
                         + (x + y <= 20.5));              //Line 5
        System.out.println("Line 6: (n >= 0) && (n <= 100) "
                         + "evaluates to "
                         + ((n >= 0) && (n <= 100)));     //Line 6
        System.out.println("Line 7: ('A' <= ch && ch <= 'Z') "
                         + "evaluates to "
                         + ('A' <= ch && ch <= 'Z'));     //Line 7
        System.out.println("Line 8: (a + 2 <= b) && !flag "
                         + "evaluates to "
                         + ((a + 2 <= b) && !flag));      //Line 8
    }
}
The console after the program finishes

Code that compares floating-point numbers.

public class FloatingPointNumbers
{
    public static void main(String[] args)
    {
        System.out.println("3.0 / 7.0 = " + (3.0 / 7.0));
        System.out.println("2.0 / 7.0 = " + (2.0 / 7.0));
        System.out.println("3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 = "
                         + (3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0));
        System.out.println("1.0 == (3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0) = "
						 + (1.0 == (3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0)));
    }
}
The console after the program finishes

The above output sows that you should be careful when comparing floating-point numbers for equality. As an alternative to testing floating-point numbers for equality, you can test that their difference is less than an acceptable level of error, e.g. 0.000001.

Previous | Compare numeric variables | Compare string variables | if/else | while | Next