06.09.2021 Views

Java with BlueJ, 2016a

Java with BlueJ, 2016a

Java with BlueJ, 2016a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

38 CHAPTER 2. BASICS<br />

Some further points about the Boolean operators (not very important to us<br />

yet):<br />

• When && is being evaluated and if the first operand is false, then the<br />

result must be false and so the second operand is not evaluated.<br />

• When || is being evaluated and if the first operand is true, then the<br />

result must be true and so the second operand is not evaluated.<br />

other operators<br />

• There are other operators (&, |, and^) that you may be interested<br />

in learning about subsequent to this course. These, amongst other<br />

features, are discussed in <strong>Java</strong> in a Nutshell [5].<br />

Relational Operators<br />

There are a number of operators defined for comparing one value to another.<br />

These are summarized in the table below (assume x and y are of type int).<br />

These operations evaluate to a boolean (true or false):<br />

Relational operators<br />

operator meaning example<br />

< less than x < y<br />

> greater than x > 5<br />

y<br />

== equal to x == y<br />

!= not equal to x != 0<br />

Note that == is the operator used to test for equality, and != is used to test<br />

two operands to determine if they are not equal. Listing 2.7 illustrates the<br />

use of relational operators. Later in the chapter on control structures we<br />

will use relational operators in many examples.<br />

Listing 2.7: Comparing char values<br />

1 public class CompareNumber<br />

2 {<br />

3 public static void main(String[] args)<br />

4 {<br />

5 // i1 and i2 are two char variables<br />

6 int i1 = 111;<br />

7 int i2 = 555;<br />

8 // Display i1 and i2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!