What is boolean operators in java

Free Java Guide

These tutorials will introduce you to Java programming Language. You’ll compile and run your own Java application, using Sun’s JDK. It’s very easy to learn java programming skills, and in these parts, you’ll learn how to write, compile, and run Java applications. Before you can develop corejava applications, you’ll need to download the Java Development Kit (JDK).


Java Boolean Operators

The Boolean logical operators are : | , & , ^ , ! , || , && , == , != . Java supplies a primitive data type called Boolean, instances of which can take the value true or false only, and have the default value false. The major use of Boolean facilities is to implement the expressions which control if decisions and while loops.

These operators act on Boolean operands according to this table

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
| the OR operator & the AND operator ^ the XOR operator ! the NOT operator || the short-circuit OR operator && the short-circuit AND operator == the EQUAL TO operator != the NOT EQUAL TO operator

Источник

Читайте также:  Php настройка переменных окружения

Java Boolean Operator Explained [Practical Examples]

Introduction to Java Boolean Operators

The java Boolean operator is also a datatype just like int, float, or char. It is used where the condition true or false is needed, where the answer needs to be either 1 or 0. 1 being true and 0 being false. By default, a Boolean variable is false, which can change afterward.

Declaration

The java Boolean operator can be declared as

boolean variable_name;

shown in the piece of code below:

Hello, the value of this bool operator is :false

In the above code, I have declared a boolean operator named as bool_operator and then assigned it a value, which is false.

Types of Java Boolean operator

In Java, there are many different types of operators that return boolean values, different types of java boolean operator is listed below:

Name Operator
Logical AND &&
Logical OR ||
Equals to !ERROR! unexpected operator ‘=’
XOR ^
NOT ^
Greater than >
Less than
Greater than or Equals to >=
Less than or Equals to
Not Equals to !=

Output of different logical operators in Java:

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

1. Logical AND Operator

The & operator and && operator both are used to check if both the expressions that are being compared are true or not, if one of them is false or both of them are false , it returns false , otherwise true . The example shown in the code attached below:

package boolean_operator; public class operator < public static void main(String[] args) < // this code snippet checks the java boolean operator && // declaring boolean variables. boolean bool_operator1= true; boolean bool_operator2=true; boolean bool_operator3=false; boolean bool_operator4=false; // the statements executed below will show different values of operands using AND conditions System.out.println("Hello, the value of this bool expression is :"+(bool_operator1&&bool_operator2));//A&&B System.out.println("Hello, the value of this bool expression is :"+(bool_operator1&&bool_operator3));//A&&B System.out.println("Hello, the value of this bool expression is :"+(bool_operator3&&bool_operator4));//A&&B System.out.println("Hello, the value of this bool expression is :"+(bool_operator2&&bool_operator4));A&&B >>

The output of the following code is

Hello, the value of this bool expression is :true Hello, the value of this bool expression is :false Hello, the value of this bool expression is :false Hello, the value of this bool expression is :false 

The only time it returns true is when both operator 1 and operator 2 are true, in all the other cases the value returned will be false.

2. Logical OR Operator

The logical OR operator is | or || it returns true only either one of the two operands are true, it returns false if both the operands are false, the given code snippet will show how logical OR operator works.

package boolean_operator; public class operator < public static void main(String[] args) < // this code snippet checks the java boolean operator || //declare boolean variables boolean bool_operator1= true; boolean bool_operator2=true; boolean bool_operator3=false; boolean bool_operator4=false; // the statements below will execute different operand values using or condition System.out.println("Hello, the value of this bool expression is :"+(bool_operator1||bool_operator2)); System.out.println("Hello, the value of this bool expression is :"+(bool_operator1||bool_operator3)); System.out.println("Hello, the value of this bool expression is :"+(bool_operator3||bool_operator4)); System.out.println("Hello, the value of this bool expression is :"+(bool_operator2||bool_operator4)); >>

The output of the following code is :

Hello, the value of this bool expression is :true Hello, the value of this bool expression is :true Hello, the value of this bool expression is :false Hello, the value of this bool expression is :true 

3. The Equal to (==) Operator

The equal to operator i.e. == returns true only if operand one and operand two are equal, in any other case it returns false. The example is shown in the code below :

package boolean_operator; public class operator < public static void main(String[] args) < // this code snippet checks the java boolean operator == // declare two variables for comparison int bool_operator1= 1; int bool_operator2=2; // the output will show if both the operands are equal or not System.out.println("Hello, the value of this bool expression is :"+(bool_operator1==bool_operator2)); >>

The output of this code is :

Since operator 1 and operator 2 are not equal, value returned is false.

Hello, the value of this bool expression is :false

4. The XOR (^) Operator

The XOR operator returns true only if the two operands are different, it returns false if the operands are same.

package boolean_operator; public class operator < // this code snippet checks the java boolean operator ^ public static void main(String[] args) < // declaring two variables boolean bool_operator1= true; boolean bool_operator2=true; // XOR operator will return true if both of them are different. System.out.println("Hello, the value of this bool expression is :"+(bool_operator1^bool_operator2)); >>

The output of the following code is false since both the operands are the same.

Hello, the value of this bool expression is :false

5. The logical NOT (!) Operator

The NOT operator returns true if the operand is false and false if the operand is true. The given code snippet will explain how the NOT operator works.

package boolean_operator; public class operator < public static void main(String[] args) < boolean bool_operator1= true; boolean bool_operator2=false; // this code snippet checks the java boolean operator ! // the output will be true if the operand is false, and false if the operand is true System.out.println("Hello, the value of this bool expression is :"+(!bool_operator1)); System.out.println("Hello, the value of this bool expression is :"+(!bool_operator2)); >>

The output of the following code is :

Hello, the value of this bool expression is :false Hello, the value of this bool expression is :true

As shown in output, it returns the reverse value of the operand.

6. Greater than (>) Operator

The greater than operator returns true if the left operand is greater than the right operand, otherwise false.

Example shown in code below

7. Less than ( <) Operator

The less than operator returns true if the left operand is less than the right operand, otherwise false.

Example shown in code below

8. Greater than or equal to (>=) Operator

The greater than or equal operator returns true if the left operand is greater than or equals to the right operand, otherwise false.

The example shown in code below

9. Less than or equal to ( <=) Operator

The greater than or equal operator returns true if the left operand is less than or equals to the right operand, otherwise false.

The example shown in code below

10. Not equal to (!=) Operator

The greater than or equal operator returns true if the left operand is not equal to the right operand, otherwise false. The example is shown in the code below.

package boolean_operator; public class operator < // this code snippet checks the java boolean operators >,=,!= public static void main(String[] args) < // declare operand 1 int operand1= 1; // declare operand 2 int operand2=2; // compare // greater than System.out.println("Hello, the value of this bool expression is :"+(operand1>operand2)); // less than System.out.println("Hello, the value of this bool expression is :"+(operand1=operand2)); // less than equals to System.out.println("Hello, the value of this bool expression is :"+(operand1 <=operand2)); // not equals System.out.println("Hello, the value of this bool expression is :"+(operand1!=operand2)); >>

The output of the following code will be as follows

Hello, the value of this bool expression is :false Hello, the value of this bool expression is :true Hello, the value of this bool expression is :false Hello, the value of this bool expression is :true Hello, the value of this bool expression is :true

Practice Code

Here is a code snippet that will help you practice the java boolean operator, determine the output of the code by dry runs and then run it on your compiler

package boolean_operator; // code to practice java boolean operator. public class operator < public static void main(String[] args) < // declaring variables to check conditions. int operand1= 1; int operand2=2; // given below are different conditions to check java boolean operator System.out.println("Hello, the value of this bool expression is :"+(operand1==operand2)); System.out.println("Hello, the value of this bool expression is :"+((operand1>operand2)&&(operand2operand2)||(operand2operand2)); System.out.println("Hello, the value of this bool expression is :"+(operand1=operand2)); System.out.println("Hello, the value of this bool expression is :"+(operand1 <=operand2)); System.out.println("Hello, the value of this bool expression is :"+(operand1!=operand2)); >>

Conclusion

Java boolean operator is widely used in programming, In most programming practices many conditional statements form the basis of logics implemented, the java conditional operators tends to form faster and efficient logics, java supports different types of logical operators that include logical operators, like the logical AND and logical OR as discussed above, unary operators like NOT and bitwise operators like >,< all of these operators are discussed in the above article.

Источник

Boolean Operators in Java

Boolean operators in Java

With the advent of technology, computers have developed, which in turn brought the requirement for programming language. Many programming languages include both low-level language as well as high-level language. High-level languages are easier to use as they are easy to understand in comparison to low-level languages. Java is one such high-level language which is used widely as a backed language for programming purpose. There are lots of a concept that one needs to study and practice to understand basic concepts. In this topic, we will be discussing Boolean operators in Java.

Web development, programming languages, Software testing & others

What is Boolean Operator?

Boolean operators are simply a set of different operators that could be used to compare expressions. Boolean operators generally have two values, either false or true. Boolean operators compare the expression of the left-hand side and the right-hand side. In comparison, it simply returns a Boolean value.

Types of Boolean Operators in Java

There are various types of Boolean operators in Java. Following are the various types of Boolean operators in Java that are most widely used.

Types of Boolean Operators in Java

1. Logical AND Operator

This is a logical assignment that use the && operator to compare logical expression. It generally gives false if any of the multiple logic fails or gives true if all the expression produces true.

Example of AND operator

Now, execute the above code

Now, execute the above code

Now, execute the above code

Now, execute the above code

2. Logical OR Operator

This is a logical assignment which uses || operator to compare logical expression. It generally gives true if any of the expression becomes true and returns false if all the expression fails.

Example of OR operator

Now, execute the above code

Now, execute the above code

Now, execute the above code

Now, execute the above code

3. Equal to Operator

This operator is used to check if operand or expression on both sides of the operator are equal or not.

Example of Equal to operator

Now, execute the above code

Now, execute the above code

4. Not Equal to Operator

This operator is used to check if operand or expression on both sides of the operator are equal or not. It produces true if operands on both sides are not the same; else gives false.

Examples of not equal to operator

Now, execute the above code

Now, execute the above code

5. Ternary Operator

This operator is used to check if else conditions. It is generally shorthand for the if-else statement. If the expression is true, then if the part is executed otherwise, else block is executed. It uses two operands which are ?:

Example of Ternary Operator

Conclusion

Java is a programming language where there are lots of concepts that one needs to study. Boolean operators are one of those. These boolean operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation, it returns the value. A boolean operator is widely used in any programming language to various logical programming expressions.

This has been a guide to Boolean operators in Java. Here we discuss the basic concept and different types of Boolean operators in Java-like Logical AND, Logical OR, Equal to, Not equal to, Ternary Operator with respective examples. You can also go through our other suggested articles to learn more –

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Источник

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