Conditions and Booleans
Subscribe to Tech with Tim
Conditions
In Java we can create statements that are known as conditions. Conditions are anything that result in a boolean value (true or false). We will have more of a use for conditions later on when we move into more complex topics like control statements and for/while loops.
Comparison Operators
To create a condition we can use comparison operators. Below is a list of the most common comparison operators in Java. We use these operators to check for equivalence or relationships between values.
> // Greater than < // Less than <= // Less than or equal to >= // Greater than or equal to == // Equivalence (is equal to) != // Not equal to
Comparing Integers
All of the operators shown above can be used to compare number (doubles and ints). Below are some examples.
int x = 0; int y = 5; double z = 34.9; boolean compare = x < y; // This is true boolean compare2 = x - 7 >= 0; // This is false boolean compare3 = z == 5; // This is false boolean compare4 = z+5 != x; // This is true
Comparing Strings
The only comparison operator that is available to use on Strings is "==". However, it does not work as you might assume. This operators does not check if two string values are the same it checks if the string objects are the same. Now you may think I've just said the same thing twice but these two things are very different. It is difficult to explain this concept without an example but essentially you can think of the == operator checking if the two strings you compare are stored in the same variable.
String x = "hello"; String y = "hello"; boolean compare = x == y; // This is false boolean compare2 = x == x; // This is true
Now this is useful to know but how can we properly compare strings? Well we need to use a method called .equals(). This method will compare string values not objects and will tell us if the two strings are equivalent.
String x = "hello"; String y = "hello"; String z = "Hello"; boolean compare = x.equals(y); // This is true boolean compare2 = x.equals(z); // This is false
CAPITALS MATTER, see in the example above how "hello" is not the same as "Hello".
Chaining Conditionals
Now that we know how to create conditions it is time to learn how to combine them. To combine conditions together and create what is known as a chained conditional we can use AND, OR and NOT. These allow us to create one large condition out of many smaller condition.
In java we use the symbols seen below to represent AND OR NOT. AND: && - if both conditions are true, return true OR: || - if at least one of the conditions is true, return true NOT: ! - if the condition is false, return true.
boolean chained = true || false; // This is true boolean chained2 = true && true; // This is true boolean chained3 = false && true; // This is false boolean chained4 = false || false; // This is false boolean chained5 = !true; // This is false boolean chained6 = !false; // This is true boolean chained7 = 5 < 3 && 6 - 9 > -80; // This is false boolean chained8 = !((!false && true) || (true)); // This is false