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

Explain javamail, Normal 0 false false false EN-IN X-...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

Is jsp technology extensible?, YES. JSP technology is extensible by the dev...

YES. JSP technology is extensible by the development of custom actions, or tags, which are encapsulated in tag libraries. #question..

Luminous Jewels, Byteland county is very famous for luminous jewels. Lumino...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

How to finding an applets size, Finding an Applet's Size When running i...

Finding an Applet's Size When running inside a web browser the size of an applet is set through the height and width attributes and cannot be changed by the applet. Several app

Railway reservation, what java stream is used for designing railway reserva...

what java stream is used for designing railway reservation form?

Explain the methods of java.lang.object, Explain The Methods of java.lang.O...

Explain The Methods of java.lang.Object? java.lang.Object gives a number of methods that are general to all objects. toString() is the most general such method. Because the def

Program of declaration of variables in java, Program of Declaration of vari...

Program of Declaration of variables in Java Program for declaring variables in Java, I've been trying so many codes for this but those codes didn't work well. Please write the

I need java expert with tomcat tuning experience, I need Java expert with t...

I need Java expert with tomcat tuning experience Project Description: Require a Java expert to fix memory issues with tomcat. Skills required: Java, Apache

Assignments, Define a function that will take a box type of object as argum...

Define a function that will take a box type of object as argument and return a box object whose dimension is 10 times more than the object

Write complete application to play game tic-tac-toe, (Tic-Tac-Toe)  Create...

(Tic-Tac-Toe)  Create class TicTacToe that will enable you to write a complete application to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array o

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