- What is logical operators in java
- Java Operator – &, && (AND) || (OR) Logical Operators
- How to use the bitwise AND operator
- How to use the logical AND operator
- How to use the logical OR operator
- Conclusion
- Logical operators in Java with Example [2023]
- What are Logical operators in Java?
- Types of Logical operators
- Logical Operators Example in Java
- Logical Operators Program in Java
- More Post on Website
- Leave a comment Cancel reply
- Check Recent Post
What is logical operators in java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Java Operator – &, && (AND) || (OR) Logical Operators
Ihechikara Vincent Abba
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 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) wp-block-code">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 wp-block-code">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
More Post on Website
Java Program to Convert Kilometers(KMS) to Miles
Leave a comment Cancel reply
Check Recent Post
- What is the Cannot find symbol Error in Java?
- How to Throw an Exception in Java?
- How long does it take to learn Python if you know Java?
- How to Move All Zeroes Present in an Array to the End in Java?
- How to Reverse the Python Dictionary?
- How to Comment Out Multiple Lines in Python?
- How to Initialize an Array in Java?
- What is Self in Python?
- How do I become an expert Python programmer?
- What is Threads on Instagram and How to use it?
- How to Find the Size or Length of an Array in JAVA?
- How to Copy an Array in JAVA?
- How to remove a character from a string in Python
- How can I remove a key from a Python Dictionary
- How to Get the Day of the Week in Python?
- How to format an instant to a String in Java
- Python Timeit Module
- Python Context Manager
- Attribute Error in Python
- How to update Python dictionary values
- How to check if key exists in a Python dictionary
- Indentation error in Python
- Python Ternary Operator
- Plus (+) operator in Python
- JavaScript parseFloat() method