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 the equals() method, Explain the equals() method The equals() m...

Explain the equals() method The equals() method of java.lang.Object acts the similar as the == operator; that is, it tests for object identity rather than object equality. The

Socket in java networking and rmi, What Is a Socket in Java Networking and ...

What Is a Socket in Java Networking and RMI? Ans) A socket is one end-point of a two-way communication link among two programs running on the network. A socket is bound to a por

Design and implement online food delivery system, You are required to desig...

You are required to design & implement online food delivery system using Java RMI technology. This involves writing both the server and the client program(s). Client programs can u

What are inner beans, When wiring beans, if a bean element is embedded to a...

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The disadvantage of this bean is that it cannot be reused any

Dropbox calendar, code in dropbox calendar and loop for feruary leapyear

code in dropbox calendar and loop for feruary leapyear

Describe member variables vs. local variables, Describe Member Variables vs...

Describe Member Variables vs. Local Variables ? class Car { String licensePlate = ""; // member variable double speed; = 0.0; // member variable double maxSp

Constructors, how do I use constructors in java and how do I apply them

how do I use constructors in java and how do I apply them

What is the difference between inner class and nested class, What is the di...

What is the difference between inner class and nested class? When a class is explained within a scope od another class, then it becomes inner class. If the access modifier 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