All logical operators in java

Operators

Now that you’ve learned how to declare and initialize variables, you probably want to know how to do something with them. Learning the operators of the Java programming language is a good place to start. Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operator Precedence

Operators Precedence
postfix expr++ expr
unary ++exprexpr +exprexpr ~ !
multiplicative * / %
additive + —
shift > >>>
relational < >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <>= >>>=
Читайте также:  Python save file with encoding

In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator » = » is far more common than the unsigned right shift operator » >>> «. With that in mind, the following discussion focuses first on the operators that you’re most likely to use on a regular basis, and ends focusing on those that are less common. Each discussion is accompanied by sample code that you can compile and run. Studying its output will help reinforce what you’ve just learned.

Previous page: Questions and Exercises: Variables
Next page: Assignment, Arithmetic, and Unary Operators

Источник

Java Logical Operators

The Boolean logical operators operate on boolean operands.

Logical Operator List

The following table lists all Java boolean logical operators.

Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
? : Ternary if-then-else

True table

The following table shows the effect of each logical operation:

A B A | B A & B A ^ B !A
False False False False False True
True False True False True False
False True True False True True
True True True True False False

Example

The following program demonstrates the boolean logical operators.

Источник

Java Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

Example

int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400) 

Java divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example Try it
+ Addition Adds together two values x + y Try it »
Subtraction Subtracts one value from another x — y Try it »
* Multiplication Multiplies two values x * y Try it »
/ Division Divides one value by another x / y Try it »
% Modulus Returns the division remainder x % y Try it »
++ Increment Increases the value of a variable by 1 ++x Try it »
Decrement Decreases the value of a variable by 1 —x Try it »

Java Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x:

Example

The addition assignment operator ( += ) adds a value to a variable:

Example

A list of all assignment operators:

Operator Example Same As Try it
= x = 5 x = 5 Try it »
+= x += 3 x = x + 3 Try it »
-= x -= 3 x = x — 3 Try it »
*= x *= 3 x = x * 3 Try it »
/= x /= 3 x = x / 3 Try it »
%= x %= 3 x = x % 3 Try it »
&= x &= 3 x = x & 3 Try it »
|= x |= 3 x = x | 3 Try it »
^= x ^= 3 x = x ^ 3 Try it »
>>= x >>= 3 x = x >> 3 Try it »
x x = x Try it »

Java Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either true or false . These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else chapter.

In the following example, we use the greater than operator ( > ) to find out if 5 is greater than 3:

Example

int x = 5; int y = 3; System.out.println(x > y); // returns true, because 5 is higher than 3 
Operator Name Example Try it
== Equal to x == y Try it »
!= Not equal x != y Try it »
> Greater than x > y Try it »
Less than x < y Try it »
>= Greater than or equal to x >= y Try it »
Less than or equal to x

Try it »

Java Logical Operators

You can also test for true or false values with logical operators.

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example Try it
&& Logical and Returns true if both statements are true x < 5 && x < 10 Try it »
|| Logical or Returns true if one of the statements is true x < 5 || x < 4 Try it »
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10) Try it »

Источник

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)

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

      Источник

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