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

Relational operators

java

+ Font mai mare | - Font mai mic



DOCUMENTE SIMILARE

Trimite pe Messenger
Logical operators
Bitwise operators
AWT Peer Interfaces
AWT Image Processing
You must create all the objects
Data Structures And Random Number Generation
Windowing Components And Layout Classes
Comments and embedded documentation
Ternary if-else operator
Business Process in Seam


Relational operators

Relational operators generate a boolean result. They evaluate the relationship between the values of the operands. A relational expression produces true if the relationship is true, and false if the relationship is untrue. The relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent ( ). Equivalence and nonequivalence works with all built-in data types, but the other comparisons wonít work with type boolean.




Testing object equivalence

The relational operators == and != also work with all objects, but their meaning often confuses the first-time Java programmer. Hereís an example:

//: Equivalence.java

public class Equivalence

} ///:~

The expression System.out.println(n1 == n2) will print out the result of the boolean comparison within it. Surely the output should be true and then false, since both Integer objects are the same. But while the contents of the objects are the same, the handles are not the same and the operators == and != compare object handles. So the output is actually false and then true. Naturally, this surprises people at first.

What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine with == and !=). Hereís how itís used:

//: EqualsMethod.java



public class EqualsMethod

} ///:~

The result will be true, as you would expect. Ah, but itís not as simple as that. If you create your own class, like this:

//: EqualsMethod2.java

class Value

public class EqualsMethod2

} ///:~

youíre back to square one: the result is false. This is because the default behavior of equals( ) is to compare handles. So unless you override equals( ) in your new class you wonít get the desired behavior. Unfortunately, you wonít learn about overriding until Chapter 7, but being aware of the way equals( ) behaves might save you some grief in the meantime.

Most of the Java library classes implement equals( ) so that it compares the contents of objects instead of their handles.






Politica de confidentialitate



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 456
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 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site