Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.6.8.2 - Numerical Equality Operators

Numerical equality operators are among the frequently applied operators, but they are also most frequent logical sources of errors. Knowing that, their application must made with care and wisdom, otherwise the program may exhibit unexpected behaviors.

If the operands are of type int or long, an integer equality test is performed. An example of integer equality test is given below:

package preliminary;
public class IntegerEquality {
public static void main(String[] args){
int firstValue= 23;
int secondValue= 23;
boolean IntegerEqualityControl= firstValue == secondValue;
System.out.println("23 == 23" + IntegerEqualityControl);
 } // end main
} //end class IntegerEquality

The result of this program :

 23 == 23 true

When the operands is a real data type like float or double, then a floating-point equality test is performed. Floating point equality test is accurately performed according the IEEE 754 standard.

If either operands is NaN, the == returns false but != returns true. If and only if the value of x is NaN, x != x returns true.

Positive and negatiave zeroes are considered equal therefore the expression,

-0.0 == 0.0

returns true.

Positive and negative infinities are considered equal to only themselves and unequal to other values.

When the restrictions about the NaN is taken into account,

An example involving a test of equality of real values, is given below:

package preliminary;
public class RealEquality {
public static void main(String[] args){
double a= 2.894583E-36;
double b= 2.894583E-36;

System.out.println(a  +  " == "  +  b  +  "  "  + (a == b));
 } // end main
} //end class RealEquality

The result of this program :

2.894583E-36 == 2.894583E-36  true

From the result of the program above, we can see that the compiler cattced the equality of two real operand of type double. It is a good news.

Let us discover about the equality test result of the real type values computed from an expression. The program is given below:

package preliminary;
public class RealExpressionsEquality {
public static void main(String[] args){
double a= 2.894583E-36;
double b= 2.894583E-36;

a = (a - (a / a) - a * a) / ((a / (1 +  a);

b = (b - (b / b) - b * b) /((b / (1 +  b);

System.out.println(a  +  " == "  +  b  +  "  "  + (a == b));
 } // end main
} //end class RealExpressionsEquality

The result of this program :

-3.454729057691557E35 == -3.454729057691557E35  true

This shows that the results of numerical expressions are computed accurately and testing their equality return an accurate result. However, equality testing of real data may not always return acuurate results. Real values are associated with a small bias and this a potencial source of wrong results in equality testing. Real numbers are advised to be compared with relational operators.

Character data is accurately tested on equality as shown below:

package preliminary;
public class CharacterEquality {
public static void main(String[] args){
char firstCharacter= 'h';
char secondCharacter= 'h';

System.out.println(firstCharacter == secondCharacter);
 } // end main
} //end class characterEquality

The result of this program :

true

Equality testing of strings is accurate as shown below:

package preliminary;
public class StringEquality {
public static void main(String[] args){

System.out.println("Bert" == "BERT");

 } // end main

} //end class StringEquality

The result of this program :

false

The program returned false because, only "BERT" == "BERT" would return true.

The program above alert us that there is very delicate points in string comparison algorithms. For having the equality confirmation, two string must be character by character equal. Any other combination will return false. This make string comparisons an area of specialization in computer programming. Matching strings is crucial, you can be rejected from a reception desk if your reservation name will not coincide with the name inscribed in your identity card. In our life, we are generally not consistent with our names. But the computer is consistent and stringent. Strings must be extensively manipulated before comparison. Every company has a special receipt for realising string comparisons as accurately possible. Whitespaces will be trimmed, characters will be transformed to upper or lower case and many other things may be realized for producing accurate results.

Another special area for string comparisons is using Regular Expressions. Regular expressions is a special method developed for string comparisons with a special coding system. Java has extensive support for regular expressions. We will cover regular expressions later in this book.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)