Logical and or operator in java

Logical Operators in Java

Logical operators can be defined as a type of operators that help us to combine multiple conditional statements. There are three types of logical operators in Java: AND, OR and NOT operators.

  • AND operator returns true when both conditions under evaluation are true , otherwise it returns false.
  • OR operator returns true if any one of the given conditions is true . OR operator returns false if and only if both conditions under evaluation are false .
  • NOT operator accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.

Introduction

You may remember AND, OR and NOT gates from your electronic and tinkering labs.

  • AND gate in electrical circuit combined two signals such that the output is on if both signals are on.
  • OR gate, on the other hand, combined signals such that the output becomes on if any of the input signals are on.
  • NOT gate reverses the input signal. (High input to low output and vice-versa)
Читайте также:  How to add date in html

and so on. Similar functionalities are performed by the logical operators in Java.

  • We can easily use logical operators to combine multiple conditions. Also whenever we tend to apply these operators on two conditions, a logical output is expected.
  • We implement them to achieve a boolean output and control the flow of execution in a program.

Let’s now have a quick look at the features of these logical operators in Java.

Features of Logical Operators In Java

  • Logical operators help us control the flow of execution of our program.
  • We have boolean operators which return only true or false .
  • Logical operators can be easily implemented onto single or multiple boolean operands.
  • There are three logical operators in Java:
    • && (Logical AND)
    • || (Logical OR)
    • ! (Logical NOT)

    Now we have a brief idea about operators, let’s see the detailed explanation and implementation of each logical operator one by one.

    AND Operator

    Syntax — cond1 && cond2

    This operator is named as the Logical AND operator. This operator returns true when both conditions under evaluation are true , otherwise it returns false.

    Short-Circuiting Effect: If the first condition cond1 evaluates to false , it doesn’t carry out a check on the second condition. It returns false without evaluating the second condition.

    We are going to understand the above-mentioned definition with the help of a table that shows us the results of the conditions in which this operator has been used. The table represents the using of AND operator.

    From the table above, we can see that the AND operator returns true only if both conditions (under consideration) are true . Even if one of the conditions is false , the AND operator returns false .

    Explanation:

    • In the code above, we are finding the maximum number out of p , q , r (taken as inputs).
    • If p is maximum among p , q , and r variables then both condition p > q and p > r should be satisfied. Thus we have combined these two conditions using the AND (&&) operator.
    • Similar logic is employed for q . If p and q are not maximum, the maximum is obviously r .

    OR Operator

    Syntax — cond1 || cond2

    This operator is named as the Logical OR operator. This operator returns true if any one of the given conditions is true. OR operator returns false if and only if both conditions under evaluation are false .

    Short-Circuiting Effect: This operator doesn’t check the second condition if the first one is true . The second condition is checked only and only if the first condition is false.

    Let’s see a table for this blog too.

    It is clear from the table above that OR operator returns false if and only if both conditions are false . Otherwise, it returns true .

    Explanation:

    • The code above takes three numbers representing the sides of a triangle as input and finds out if the triangle is valid triangle or not.
    • To solve this, we have used the property: «Sum of two sides of a triangle is always greater than the third side.»
    • Hence, if sum of any two sides of the input three sides is less than or equal to the third side, the triangle cannot exist.
    • Thus, we have used the OR operator as it returns true if any of the conditions are true .

    NOT Operator

    Syntax — !

    This operator is called as Logical NOT operator. This can be used using an exclamation ( ! ) mark. It accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.

    We can make a similar truth table for this operator itself and let’s see how it would look like.

    Here, if the condition is true then the operator returns false i.e. the opposite of true and vice versa.

    Explanation:

    • In the code above, we are simply printing the output of NOT operator on the two conditions: (p < q) and (p >q) .

    XOR Operator

    Syntax — ^

    This is a bitwise operator and stands for «exclusive or» . It performs logical operations as well if the operands are boolean variables.

    From the table above, it is clear that when both the inputs are identical, false is returned. However, if XOR is performed on opposite operands, true is returned.

    Logical Operators Example

    Logical Operators Table

    Operator Example Description
    Conditional AND cond1 && cond2 Returns true only if both cond1 and cond2 are true
    Conditional OR cond1 || cond2 Returns true if atleast one of cond1 and cond2 is true
    Logical NOT !cond Returns the opposite of input argument cond
    Logical XOR cond1 ^ cond2 Returns true only if cond1 and cond2 are different

    Summary

    • Logical operators can be defined as a type of operators that help us to combine multiple conditional statements.
    • There are three types of logical operators in Java:
      • AND Operator (&&)
      • OR Operator (||)
      • NOT Operator (!)

      Also Read

      Источник

      Java Operator – &, && (AND) || (OR) Logical Operators

      Ihechikara Vincent Abba

      Ihechikara Vincent Abba

      Java Operator – &, && (AND) || (OR) Logical Operators

      We use operators in most programming languages to perform operations on variables.

      They are divided into various categories like arithmetic operators, assignment operators, comparison operators, logical operators, and so on.

      In this article, we will be talking about the bitwise AND operator, and the AND ( && ) and OR ( || ) logical operators.

      How to use the bitwise AND operator

      The symbol & denotes the bitwise AND operator. It evaluates the binary value of given numbers. The binary result of these numbers will be returned to us in base 10.

      When the & operator starts its operation, it will evaluate the value of characters in both numbers starting from the left.

      Let’s look at an example to help you understand better:

      System.out.println(10 & 12); // returns 8

      The binary value of 10 is 1010

      The binary value of 12 is 1100

      Here is something you should have in mind before we start the operation:

      So let’s carry out the operation.

      The first character for 10 is 1 and the first character for 12 is also 1 so:

      We move on to the second characters – 0 for 10 and 1 for 12:

      For the third characters – 1 for 10 and 0 for 12:

      For the fourth characters – 0 for 10 and 0 for 12:

      Now let’s combine all the returned characters. We would have 1000.

      The binary value 1000 in base 10 is 8 and that is why our operation returned 8.

      How to use the logical AND operator

      Note that we use logical operators to evaluate conditions. They return either true or false based on the conditions given.

      The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.

      Here is what the syntax looks like:

      statment1/condition1 && statemnt2/condition2

      As you can see above, there are two statements/conditions separated by the operator. The operator evaluates the value of both statements/conditions and gives us a result – true or false.

      System.out.println((10 > 2) && (8 > 4)); //true

      The operation will return true because both conditions are true – 10 is greater than 2 and 8 is greater than 4. If either one of the conditions had an untrue logic then we would get false .

      To better understand the && operator, you should know that both conditions must be true to get a value of true .

      Here is another example that returns false :

      System.out.println((2 > 10) && (8 > 4)); // false

      Here, 2 is not greater than 10 but 8 is greater than 4 – so we get a false returned to us. This is because one of the conditions is not true.

      • If both conditions are true => true
      • If one of the two conditions is false => false
      • If both conditions are false => false

      How to use the logical OR operator

      We use the symbol || to denote the OR operator. This operator will only return false when both conditions are false. This means that if both conditions are true, we would get true returned, and if one of both conditions is true, we would also get a value of true returned to us.

      statment1/condition1 || statemnt2/condition2

      Let’s go over a few examples.

      System.out.println((6 < 1) || (4 >2)); // true

      This returns true because one of conditions is true.

      • If both conditions are true => true
      • If one of the conditions is true => true
      • If both conditions are false => false

      Conclusion

      In this article, we learned how to use the bitwise & operator in Java and how the operation is carried out to give us a result.

      We also learned how to use the && and || logical operators in Java. We learned what value each operation returns based on the conditions involved in the operation.

      Источник

      Logical operators in Java with Example [2023]

      Logical Operators in Java

      Logical operators in java are the building blocks used to perform functions on variables and values. In this post, you can find logical operators example in Java. We can use many different operators according to our needs for calculations and functions. Operators in java can be logical, ternary, bitwise arithmetic, relational operators etc. Though they are called logical operators, users can apply them on any type. It is not necessary to perform the operation on Boolean values only.

      What are Logical operators in Java?

      Java Logical Operators perform operations such as AND, OR, NOT. These functions are similar to AND gates or OR gates in electronics. They help in combining two conditions to make one final output. We always keep in mind the short-circuiting effect which says that the second value is never evaluated if the first condition is false. Use of logical operators mainly for decision making. We can also use it in places where we have to get results as explicit true or explicit false.

      Types of Logical operators

      1.) Logical AND operator: This operator tends to return a true value if the satisfactory conditions are true. If one of the 2 values is false, it returns false. However, returns true if both the conditions are true. It is denoted by &&.

      2.) Logical OR operator in Java: We use vertical lines to represent the operator. It returns true if one of the two conditions is true. If even one condition is true, the output will be true. For false, both conditions need to be false. It is denoted by ||

      3.) Logical NOT operator: The operator returns the exact opposite value of the condition. For example, if the condition is true, the output of the “NOT” operator will be false. And if the condition will be false, the output of the “NOT” operator will be true. It will simply negate a statement. The statement below means when the condition is not (!) true, t is false. And when the condition is not(!) false, it is true.

      Logical Operators Example in Java

      public class Test < public static void main(String args[]) < boolean x = true; boolean y = false; System.out.println("x && y = " + (x&&y)); System.out.println("x || y = " + (x||y) ); System.out.println("!(x && y) https://www.jdoodle.com/a/6qdg" rel="nofollow noopener" target="_blank">

      The output of the above java program will be:

      x && y = false x || y = true !(x && y) = true

      Logical Operators Program in Java

      public class DeveloperHelps < public static void main(String[] args) < int a = 25; int b = 40; 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= " + ~a); boolean a1 = true; boolean a2 = true; boolean a3 = false; System.out.println("val1 & val2= " + (a1 & a2)); System.out.println("val2 & val3= " + (a2 | a3)); System.out.println("val2 | val3= " + (a2 | a3)); System.out.println("val1 ^ val2= " + (a1 ^ a2)); System.out.println("!val1 https://www.jdoodle.com/a/6qdh" rel="nofollow noopener" target="_blank">

      The output of the above java program will be:

      a & b= 8 a | b= 57 a ^ b= 49 ~a= -26 val1 & val2= true val2 & val3= true val2 | val3= true val1 ^ val2= false !val1= false

      Fuel Your Passion for Coding: Dive into Our Other Posts

      Java Program to Convert Kilometers(KMS) to Miles

      Источник

Оцените статью