Operators

We can use logical and relational operators to create comparisons. For example, if x=5 and y=7, the comparison (x>=y) is false, but (y>x) is true. Java uses the typical relational operators to compare primitive values, including the use of != and ==.

It is important to note that while simple comparisons can be written for all variable types in Java, the relational operators should be reserved for use with primitive variable types. To compare reference types, or objects, you will need to use the equals() and compareTo() methods. For example, to test if two strings are the same, we must use the following: s1.equals(s2);
or to see if s1 is smaller than s2, we must use the following: s1.compareTo(s2)<0

Sometimes, one relation is not sufficient as a comparison. For example: given x, y, and z, we need to determine if x is the smallest. So, we need to test to see if x
In evaluating compound comparisons, Java employs a tactic called short circuit evaluation. This is best seen by example. Assume Java is evaluating ((5<7 || (6>8)). Remember that || requires only one part to be true to make the entire statement true. So, when Java encounters 5<7 and determines it as true, it need not (and consequently does not) check the truth of (6>8) because it’s truth value is irrelevant to the value of the statement. Conversely, in a conjunctive statement, Java short circuits if the first part is false.

Conditions

The keyword ‘if’ signifies a conditional statement in java. A condition in java can be written as a simple if or as an if/else statement. In these cases, when the condition is true, the if block is executed. When the condition is false, the else block, if present, is executed. Here is an example that checks a string to see if it is equal to “password”: if(input.equals(“password”)) System.out.println(“Welcome!!”); else System.out.println(“Sorry, your password is incorrect!”);
Sometimes, a more complex system of conditions is necessary. Nested conditions are when a condition is placed in another condition, either inside the if block or in the else block. This type of nesting can go very deep, depending on the desired options for the user. Often times, when nesting in the else block, we find that we are generating a list of mutually-exclusive events (events where one or the other, but not both can occur). When this is the case, we use an else-if chain: if(score>=9) letter=’A’; else if(score>=8) letter=’B’; else if(score>=7) letter=’C’; else if(score>=6) letter=’D’; else letter=’F’;