Explain the order of evaluation of logic operators, JAVA Programming

Assignment Help:

Explain the Order of Evaluation of Logic Operators ?

When Java sees a && operator or a ||, the expression on the left side of the operator is evaluated first. For instance, consider the subsequent:
boolean b, c, d;
b = !(3 > 2); // b is false
c = !(2 > 3); // c is true
d = b && c; // d is false

While Java evaluates the expression d = b && c;, it first checks whether b is true. Here b is false, so b && c must be false regardless of while c is or is not true, so Java doesn't bother checking the value of c.

On the other hand while faced along with an || Java short circuits the evaluation as soon as it encounters a true value because the resulting expression must be true. This short circuit evaluation is less significant in Java than in C because in Java the operands of && and || must be booleans that are unlikely to have side effects that depend on whether or not they are evaluated. Still it's likely to force them. For example consider this code.

boolean b = (n == 0) || (m/n > 2);
Even if n is zero this line will never cause a division through zero, because the left hand side is always evaluated first. If n is zero then the left hand side is true and there's no required to evaluate the right hand side. Mathematically this forms sense because m/0 is in some sense infinite that is greater than two.

This isn't a perfect solution by since m may be 0 or it may be negative. If m is negative and n is zero then m/n is negative infinity that is less than two. And if m is also zero, then m/n is very undefined.

The proper solution at this point depends on your problem. Since real world quantities aren't infinite, while infinities start popping up within your programs, nine times out of ten it's a sign in which you've lost too much precision. The remaining times are commonly signals that you've left out some little factor in your physical model in which would erase the infinity.

Thus if there's a real chance your program will have a divide by zero error think carefully about what it means and how you should respond to it. If, upon reflection, you decide in which what you actually need to know is whether m/n is finite and greater than zero you should use a line like this

boolean b = (n != 0) && (m/n > 0);


Related Discussions:- Explain the order of evaluation of logic operators

Explain code and document bases, Explain Code and Document Bases ? If ...

Explain Code and Document Bases ? If you don't know the exact URL of the image, but you do know its name and in which it's in the similar directory as the applet, you can use

Sequence diagrams , Sequence diagrams are communication diagrams which deta...

Sequence diagrams are communication diagrams which detail what messages are sent and when. The sequence diagrams are placed according to time. The time performs as you move from to

Explain the essential api concepts associated with j2me, Question : (a)...

Question : (a) Class file verification in CLDC is different from class file verification in J2SE. Explain and discuss how and why it is different, illustrating your answer

Regex , Having trouble with a lab, need to

Having trouble with a lab, need to

Explain multi dimentional arrays in java, Explain multi dimentional arrays ...

Explain multi dimentional arrays in java? So far all these arrays have been one-dimensional. That is, a single number could locate any value in the array. Therefore sometimes d

Explain bean lifecycle in spring framework, 1. The spring container searche...

1. The spring container searches the bean's definition from the XML file and instantiates the bean. 2. Using the dependency injection, spring populates all of the properties as

Explain booleans in java, Explain Booleans in java? Booleans are named ...

Explain Booleans in java? Booleans are named after George Boole, a nineteenth century logician. Every boolean variable has one of two values, true or false. These are not the s

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

What is an xml namespace?, Question 1 What is Response Redirection? Explai...

Question 1 What is Response Redirection? Explain in brief Question 2 What is JDBC? Explain Question 3 Write a short note on Expressions Question 4 What is an XML na

What is spring configuration file, Spring configuration file is an XML file...

Spring configuration file is an XML file. This file having the classes information and defines how these classes are configured and introduced to each other.

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