and/or/not: Logical Operators in Java

Logical Operator Or

We need to check more than just one conditional expression, for example if the classroom number is 101 or 102, the building where the classroom is in is Building East Wing. This is an example of the logical or – either classroom 101 or classroom 102.

Java uses the sign for the logical or is one or two vertical bars (| or ||). If any of the two conditions is true, the result of the entire expression is also true. In the classroom example, we use a variable of type String, which has a method equals() that compares the values of two strings. we can see whether the value of the variable classroomNumber is “101” or “102”:

if (classroomNumber.equals("101") | classroomNumber.equals("102"))
{
   // Building East Wing
}

This if statement can use two vertical bars:

if (classroomNumber.equals("101") | classroomNumber.equals("102"))
{ 
    // Building East Wing
}

The difference is that if you use two bars (||), and the first expression is true, the second condition is not checked. If placing just a single bar (|), JVM evaluates both condition.

Logical Operator And

The logical and is described by one (&) or two ampersands (&&) and each condition in the parentheses must be true to make the entire condition astrue. For example, if you registered Java class, and the class level is starter, then you must be in classroom 101

if (className.equals("Java") && classLevel.equals("starter"))
{
   // classroom number is 101
}

or

if (className.equals("Java") && classLevel.equals("starter"))
{
   // classroom number is 101
}

If you use double ampersands (&&) and the first condition is false, the second one will not even be checked, because the entire condition result is false. Using the single ampersand (&) both conditions have to be evaluated.

Logical Operator Not

The Logical Operator not is also known as negation and is described using the exclamation point (!). The Logical Operator not changes condition to the opposite meaning. For example, if the classroomNumber is NOT 101, the rest classrooms have printers in the room, therefore:

if (!classroomNumber.equals("101")) {
   // a printer in room
}

The following two expressions will produce the same result, because more than 100 and not less than or equal to 100 have the same meaning:

if (price > 100) {
   // do something
}

if (!(price <= 100)) {
   // do something
}

Practice:

Please run the code below and get the feeling about what the logical Operator’s relationship.

public class MultiFunction{

   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;

      System.out.println("a && b = " + (a&&b));
      System.out.println("a || b = " + (a||b) );
      System.out.println("!(a && b) = " + !(a && b));
      System.out.println("!(a || b) = " + !(a || b));
    } 

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s