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

Relate java with JVM, Relate java with JVM Java performance gets better...

Relate java with JVM Java performance gets better each year as JVM gets smarter. This works, since making JVM smarter doesn't require any great change to the java language, sou

Java., the textbok is Introduction to Java™ Programming, Brief Version, Eig...

the textbok is Introduction to Java™ Programming, Brief Version, Eighth Edition Week 8 Exercises Chapter 8 Programming Exercises from Pages 295 - 299. Do Exercise Problems 2,

Card, Create a class called DeckOfCards that stores 52 objects of the Card ...

Create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a deck, and report the number of cards left in the deck. The s

What is jms consumer, JMS consumer is a JMS client that receives messages.

JMS consumer is a JMS client that receives messages.

What are the different parts of autoproxying, a)  BeanNameAutoProxyCreator ...

a)  BeanNameAutoProxyCreator b) DefaultAdvisorAutoProxyCreator c)  Metadata autoproxying

Use case diagram, Depicts the typical communication between external users ...

Depicts the typical communication between external users and the system. The emphasis is on what a machine does rather than how it works it. A use case is a summary of scenarios fo

Explain the concept of inheritance, Question: (a) (i) Explain the conc...

Question: (a) (i) Explain the concept of inheritance. Illustrate your answer with a suitable Java example. (ii) Java does not support multiple inheritance but does provide

I need social mobile application, Project Description: I'm using a php s...

Project Description: I'm using a php script called phpfox on my site its work as social network . So I need is to use this script as CMS for my application. What users wil

Explain the concept of multi-threaded- java, Explain the concept of Multi-T...

Explain the concept of Multi-Threaded- JAVA Java has a concept of concurrency wired right in to the language itself. This works out more cleanly than languages where concur

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