Describe logical operators in java, JAVA Programming

Assignment Help:

Describe Logical Operators in Java ?

The relational operators you've learned so far (<, <=, >, >=, !=, ==) are enough while you only required to check one condition. Therefore what if a particular action is to be taken only if various conditions are true? You can use a series of if statements to test the conditions, as follow:

if (x == 2) {
if (y != 2) {
System.out.println("Both conditions are true.");
}
}

This, therefore, is hard to write and harder to read. It just gets worse as you add more conditions. Providentially, Java gives an easy way to handle multiple conditions: the logic operators. There are three logic operators, &&, || and !.
&& is logical and. && merges two boolean values and returns a boolean that is true if and only if both of its operands are true. For example

boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
|| is logical or. || combines two boolean variables or expressions and returns a result in which is true if either or both of its operands are true. For example

boolean b;
b = 3 > 2 || 5 < 7; // b is true
b = 2 > 3 || 5 < 7; // b is still true
b = 2 > 3 || 5 > 7; // now b is false
The last logic operator is ! which means not. It reverses the value of a boolean expression. Thus if b is true !b is false. If b is false !b is true.

boolean b;
b = !(3 > 2); // b is false
b = !(2 > 3); // b is true
These operators permit you to test multiple conditions more simply. For instance the previous example can now be written as
if (x == 2 && y != 2) {
System.out.println("Both conditions are true.");
}
That's a lot clearer.


Related Discussions:- Describe logical operators in java

What are not allowed within the ejb container? , In  order  to  provide  po...

In  order  to  provide  portable  and  reliable  EJB  elements,  the  following  restrictions  apply  to  EJB  code implementation: 1. Avoid using static non-final fields. Defin

What is switchaction, The SwitchAction class gives a means to switch from a...

The SwitchAction class gives a means to switch from a resource in one module to another resource in a dissimilar module. SwitchAction is useful only if you have multiple modules in

I need viral script for facebook likes, Project Description: I need a Sc...

Project Description: I need a Script developer who can make script for me. Need urgently. Skills required: Java, Facebook Marketing, Javascript, PHP, Script Install

How to returning multiple values from methods, How to Returning Multiple Va...

How to Returning Multiple Values From Methods ? It is not probable to return more than one value from techniques. You cannot, for example, return the licensePlate, speed and m

Error, UnsupportedClassVersionError

UnsupportedClassVersionError

Explain overriding methods and the solution, Explain Overriding Methods: Th...

Explain Overriding Methods: The Solution The object oriented solution to this problem is to describe a new class, call it SlowCar, that inherits from Car and imposes the additi

Which are three elements of event handler, The event handler attribute cons...

The event handler attribute consists of three elements. Write about each of them? 1. The identifier of the event handler. 2. The equal sign. 3. A string consisting of JavaScrip

Describe validate() and reset() methods, Validate() : Used to validate prop...

Validate() : Used to validate properties after they have been populated; known as before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Followin

Online doctor, can you explain me the er diagram for the online doctor syst...

can you explain me the er diagram for the online doctor system

What is jms client, An application or procedure that produces and/or receiv...

An application or procedure that produces and/or receives messages.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd