Anda di halaman 1dari 3

Java Notes 2017

Generalizations
Congratulations! You've learned some of the building blocks of Java programming. What
can we generalize so far?
 Data Types are int, boolean, and char.
 Variables are used to store values.
 Whitespace helps make code easy to read for you and others.
 Comments describe code and its purpose.
 Arithmetic Operators include +, -, *, /, and %.
 Relational Operators include <, <=, >, and >=.
 Equality Operators include == and !=.
A full understanding of these concepts is key to understanding the remainder of the Java
course. Let's keep going!
Boolean Operators: &&
Let's look at a set of operators that let us use control flow in our programs. These operators
are called Boolean operators.
There are three Boolean operators that we will explore. Let's start with the first one: and.
1. The and operator is represented in Java by &&.
2. It returns a boolean value of true only when the expressions on both sides of && are
true.

For example, the code below shows one outcome of the Boolean operator &&:
// The following expression uses the "and" Boolean operator System.out.println(true && true); //
prints true
The code below shows the rest of the possible outcomes of the Boolean operators: &&:
// The following expressions use the "and" Boolean operator System.out.println(false && false); //
prints false System.out.println(false && true); // prints false System.out.println(true && false);
// prints false
We can also use the Boolean operator && with Boolean expressions such as the following:
System.out.println(2 < 3 && 4 < 5);
The example above will print out true because the statements "2 is less than 3" and "4 is
less than 5" are both true.

Boolean Operators: ||
Great! The second Boolean operator that we will explore is called or.
1. The or operator is represented in Java by ||.
2. It returns a Boolean value of true when at least one expression on either side of || is
true.

The code below shows all the outcomes of the Boolean operator ||:
//The "or" Boolean operator: System.out.println(false || false); // prints false
System.out.println(false || true); // prints true System.out.println(true || false); // prints true
System.out.println(true || true); // prints true
We can also use the Boolean operator || with Boolean expressions such as the following:
System.out.println(2 > 1 || 3 > 4);
The example above will print out true because at least one statement — "2 is greater
than 1" — is true even though the other statement — "3 is greater than 4" — is false.
Boolean Operators: !
Fantastic! The final Boolean operator we will explore is called not.
1. The not operator is represented in Java by !.
2. It will return the opposite of the expression immediately after it. It will return false if
the expression is true, and true if the expression is false.

The code below shows all the outcomes of the Boolean operator !:
//The "not" Boolean operator: System.out.println(!false); // prints true System.out.println(!true); //
prints false
We can also use the Boolean operator ! with Boolean expressions such as the following:
System.out.println( !(4 <= 10) );
The example above will print out false because the statement "4 is less than or equal
to 10" is true, but the ! operator will return the opposite value, which is false.

Boolean Operators: Precedence


The three Boolean operators &&, ||, and ! can also be used together and used multiple
times to form larger Boolean expressions.
However, just like numerical operators, Boolean operators follow rules that specify the
order in which they are evaluated. This order is called Boolean operator precedence.
The precedence of each Boolean operator is as follows:
1. ! is evaluated first
2. && is evaluated second
3. || is evaluated third

Like numerical expressions, every expression within parentheses is evaluated first.


Expressions are also read from left to right.
The following statement demonstrates how Boolean operator precedence works:
System.out.println( !(false) || true && false);
The example above will print out true. In order, the expression is evaluated as follows:
1. First, the ! Boolean operator in !(false) returns true.
2. Second, true && false evaluates to false.
3. Finally, the remaining expression true || false evaluates to true.

Anda mungkin juga menyukai