Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Logical operators

java



+ Font mai mare | - Font mai mic



Logical operators

The logical operators AND (&&), OR (||) and NOT (!) produce a boolean value of true or false based on the logical relationship of its arguments. This example uses the relational and logical operators:

//: Bool.java



// Relational and logical operators

import java.util.*;

public class Bool

static void prt(String s)

} ///:~

You can apply AND, OR, or NOT to boolean values only. You can't use a non-boolean as if it were a boolean in a logical expression as you can in C and C++. You can see the failed attempts at doing this commented out with a //! comment marker. The subsequent expressions, however, produce boolean values using relational comparisons, then use logical operations on the results.

One output listing looked like this:

i = 85

j = 4

i > j is true

i < j is false

i >= j is true

i <= j is false

i == j is false

i != j is true

(i < 10) && (j < 10) is false

(i < 10) || (j < 10) is true

Note that a boolean value is automatically converted to an appropriate text form if it's used where a String is expected.

You can replace the definition for int in the above program with any other primitive data type except boolean. Be aware, however, that the comparison of floating-point numbers is very strict. A number that is the tiniest fraction different from another number is still "not equal." A number that is the tiniest bit above zero is still nonzero.

Short-circuiting

When dealing with logical operators you run into a phenomenon called "short circuiting." This means that the expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined. As a result, all the parts of a logical expression might not be evaluated. Here's an example that demonstrates short-circuiting:



//: ShortCircuit.java

// Demonstrates short-circuiting behavior

// with logical operators.

public class ShortCircuit

static boolean test2(int val)

static boolean test3(int val)

public static void main(String[] args)

} ///:~

Each test performs a comparison against the argument and returns true or false. It also prints information to show you that it's being called. The tests are used in the expression:

if(test1(0) && test2(2) && test3(2))

You might naturally think that all three tests would be executed, but the output shows otherwise:

test1(0)

result: true

test2(2)

result: false

expression is false

The first test produced a true result, so the expression evaluation continues. However, the second test produced a false result. Since this means that the whole expression must be false, why continue evaluating the rest of the expression? It could be expensive. The reason for short-circuiting, in fact, is precisely that; you can get a potential performance increase if all the parts of a logical expression do not need to be evaluated.





Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 631
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved