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

Program with eclipse and enumeration , Problem Definition A new Met Of...

Problem Definition A new Met Office web application will allow users of their web site to view rainfall statistics for months and years in the UK. The application allows the m

What is portableremoteobject.narrow() method , What is PortableRemoteObject...

What is PortableRemoteObject.narrow() method and what is used for?                Ans) Java RMI-IIOP gives a mechanism to narrow the the Object you have received from from your

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

Describe graphics objects, Describe Graphics Objects ? In Java all draw...

Describe Graphics Objects ? In Java all drawing takes place via a Graphics object. This is an example of the class java.awt.Graphics. Initially the Graphics object you use w

Unicode-ascii utf16-utf 8, How many bits are we used in Unicode, ASCII, U...

How many bits are we used in Unicode, ASCII, UTF-16, and UTF-8 characters?

Develop an online website using java, Project Description: I am planning...

Project Description: I am planning to prepare a website which caters the services to online internet users. I have already prepared most HTML5 pages by own and wanted to impl

Catch clause should be used to handle the exception, How does a try statem...

How does a try statement determine which catch clause should be used to handle the exception?

Url instance and url connection instance, Difference between URL instance a...

Difference between URL instance and URL Connection instance? Ans) A URL instance shows the location of a resource, and a URL Connection instance shows a link for accessing or co

Describe unbalanced arrays, Describe Unbalanced Arrays ? Such as C Java...

Describe Unbalanced Arrays ? Such as C Java does not have true multidimensional arrays. Java fakes multidimensional arrays by using arrays of arrays. This means that it is prob

Produce solutions to modern-practical problems, Students will design, devel...

Students will design, develop, test and document a Java application that reads data from an input file, processes the data, and then writes the processed data to an output file. CM

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