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

Derfine j2ee in detail?, A J2EE component is a self-contained functional so...

A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related files and classes and interacts with other components. The

Payroll calculator, how to write aprogramm that calculates gross pay, taxe...

how to write aprogramm that calculates gross pay, taxes, and net pay for both hourly and commission employees. It will then display those results to the screen

What are the various struts tag libraries, The Struts tag libraries are: ...

The Struts tag libraries are: ? HTML Tags ? Logic Tags ? Template Tags ? Bean Tags ? Tiles Tags ? Nested Tags

What type of information can be stored in a database, What type of informat...

What type of information can be stored in a database? In a database we can store several types of informations like: • Numbers, Booleans, text • Sounds • Images • Video

Continue statement in javascript, It terminates execution of the block of s...

It terminates execution of the block of statements in while or for loop & continues execution of the loop along with the next iteration. On the contrary to the break statement, con

Preventing applet based social engineering attacks, Preventing Applet Based...

Preventing Applet Based Social Engineering Attacks To help avoid this, Java applet windows are specifically labeled as such along with an ugly bar that says: "Warning: Applet W

Function and array in java script, Specifications 1. readNumberOfEntri...

Specifications 1. readNumberOfEntries → This function will ask the user for a value greater than or equal to two. The function will keep asking values as long as the user prov

In programming what is a loop, In programming, what is a loop? A loop i...

In programming, what is a loop? A loop is a programming language statement that permits your code to be repeatedly executed LOOP is a pedagogical programming language designed

La food, The fancy new French restaurant La Food is very popular for its au...

The fancy new French restaurant La Food is very popular for its authentic cuisine and high prices. This restaurant does not take reservations. To help improve the efficiency of the

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