Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Boolean Operators in java tutorial and MCQ

Boolean Operators in java

Study and learn Java MCQ questions and answers on Java Boolean Logical Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions.

Boolean Operators in java

Java Boolean Logical Operators work with boolean data type values. Some logical operators work with a single Operand while others work with two Operands. There are bitwise logical operators too which we discuss later.

Boolean Logical Operators with Priority in java

The list of Boolean Logical operators is given below. Note that the Logical Unary NOT (!) has got the highest priority among all other logical operators. The assignment operator has the least priority.

Note 1: Except for Logical NOT(!), all other logical operators have less priority than Arithmetic and Relational operators.

PriorityOperatorSimple Name
1!Logical Unary NOT
2&Logical AND
3^Exclusive OR or XOR
4|Logical OR
5&&Logical Short Circuit AND
6||Logical Short Circuit OR
7op=Compound Assignment Operator&=, |=, ^=
7=Assignment

Boolean Operators

Note 2: Generally, software programmers use only Short Circuit Logical operators compared to normal logical operators for good performance and to differentiate bitwise AND and OR from Logical AND and OR operators. If you use AND (&) operator, it generally means you are using it in the context of bitwise operations.

Example with Precedence or Priority Explained

In the below code example, Relational operator (>) takes higher priority. Short Circuit AND (&&) has got the least priority.

class OperatorPrecedence
{
  public static void main(String args[])
  {
    int a=5;
    int b=9;
    boolean c = a > 4 && true & b !=9;
    // Priority order: >, !=, &, &&
    // (a>4) && true & b!=9
    // true && true & b==9
    // true && true & false
    // true && false
    //false
    System.out.println(c);
  }
}
//OUTPUT: false

1. Logical Unary NOT (!) Operator

Logical Unary NOT operator requires just One Operand. It simply turns true to false and false to true. Its symbol is an Exclamation.

ExpressionResult
falsetrue
truefalse

Java Boolean Logical Operators

Example Usage:

 boolean show = true;
 show = !show;
 if(!show)
 {
   System.out.println("SHOW = false");
 }

2. Logical AND (&) Operator

Logical AND operator gives an output of true only if both operands are true. Logical AND operator checks the value of both operands or expressions before giving output. Even if the first expression is false, it goes for evaluating the second expression. This is a time waste thing and a drawback. Its symbol is an Ampersand.

expression1expression2Result
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Example Usage:

 int k=5, p=9;
 c = (k<4)&(p>5);
 //p>5 is evaluated. Time waste.
 if(c)
 {
   System.out.println("Logical AND");
 }

3. Logical Short Circuit AND (&&) Operator

Logical Short Circuit AND gives an output of true only if both Operands or Expressions are true. If the first expression is false, Short Circuit AND (&&) avoids evaluating the second expression. So it is faster than normal Logical AND (&) operator. Its symbol is Two Ampersands (&&).

expression1expression2Result
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Example Usage:

 int k=5, p=9;
 c = (k<4)&&(p>5);
 //p>5 is not evaluated.
 if(!c)
 {
   System.out.println("Short Circuit Logical AND");
 }

4. Logical OR (|) Operator

The logical OR (|) operator gives an output of true if one of the operands is true. So a combination of true and false always gives true as the output. Logical OR evaluates the second expression or operand even if the first expression is true. So it is slow compared to Short Circuit Logical Operator (||). Its symbol is a PIPE.

expression1expression2Result
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

Example Usage:

 int k=5, p=9;
 c = (k>3)|(p>5);
 //p>5 is evaluated. Time waste.
 if(c)
 {
   System.out.println("Logical OR");
 }

5. Logical Short Circuit OR (||) Operator

The Logical Short Circuit OR (||) operator gives an output of true if one of the expressions or operands is true. It gives an output of true if the first expression is true without evaluating the second expression. So it is fast. Only if the first expression is false, the second expression is evaluated or executed by Short Circuit OR operator.

expression1expression2Result
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

Its symbol is Two Pipes (||).

Example Usage:

 int k=5, p=9;
 c = (k>3)||(p>5);
 //p>5 is not evaluated.
 if(c)
 {
   System.out.println("Short Circuit Logical OR");
 }

6. Logical Exclusive OR (^) Operator

The Logical Exclusive OR (^) operator gives an output of true if both operands are different. It gives an output of false if both the operands or expressions are the same. Its symbol is a CARAT.

expression1expression2Result
falsefalsefalse
falsetruetrue
truefalsetrue
truetruefalse

Example Usage:

 int k=5, p=9;
 c = (k>3)^(p>9);
 if(c)
 {
   System.out.println("Logical XOR");
 }

 

Java Compound Logical Assignment Operators

There are only three Compound Logical Assignment operators in Java, AND, OR and Exclusive OR. Simply put an Equal To (=) symbol after the Logical AND, OR or XOR operators to turn it into a Compound Assignment operator.

Note: The compound assignment is not possible with Short Circuit AND (&&), Short Circuit OR (||) and Unary NOT(!) operators.

1. Compound Assignment AND (&=) Operator

Here “expression” should be boolean variable or constant. “var” is a variable.

Syntax:

var &= expression
var = var & expression
eg.
a &= false
a = a & false

2. Compound Assignment OR(|=) Operator

Here “expression” should be boolean variable or constant. “var” is a variable.

Syntax:

var |= expression
var = var | expression
eg.
a |= true
a = a | true

3. Compound Assignment Exclusive OR (^=) Operator

Here “expression” should be boolean variable or constant. “var” is a variable.

Syntax:

var ^= expression
var = var ^ expression
eg.
a ^= true
a = a ^ true

Example: Logical Compound Assignment Operators

class CompoundAssignmentOperators
{
  public static void main(String args[])
  {
    boolean a = true;
    boolean b= false;
    boolean c = true;
    boolean d = false;
    a &= false; //a = a & false = false
    b |= true; //b = b | true = true
    c ^= false; //c = c^false = true
    d &&= false; //Error. Not an Operator
    d ||= false; //Error
  }
}

Java Boolean Logical Operators online test

[WpProQuiz 69]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.