These operators can be used to compare values of any of the numeric types.
They can also be used to compare values of type char. For characters, < and > are defined according the numeric Unicode values of the characters.
By the way, the operators == and != can be used to compare boolean values too. This is occasionally useful. For example:
boolean sameSign;
sameSign = ( (x > 0) == (y > 0) );
One thing that you cannot do with the relational operators <, >, <=, and >= is to use them to compare values of type String.
You can legally use == and != to compare Strings, but because of peculiarities in the way objects behave, they might not give the results you want.
Instead, you should compare strings using subroutines such as equals() and compareTo()
Another place where == and != don’t work as you would expect is with Double.NaN, the constant that represents an undefined value of type double.
They can also be used to compare values of type char. For characters, < and > are defined according the numeric Unicode values of the characters.
(This might not always be what you want. It is not the same as alphabetical order because all the upper case letters come before all the lower case letters.)You can also assign boolean-valued expressions to boolean variables, just as you can assign numeric values to numeric variables. And functions can return boolean values.
By the way, the operators == and != can be used to compare boolean values too. This is occasionally useful. For example:
boolean sameSign;
sameSign = ( (x > 0) == (y > 0) );
One thing that you cannot do with the relational operators <, >, <=, and >= is to use them to compare values of type String.
You can legally use == and != to compare Strings, but because of peculiarities in the way objects behave, they might not give the results you want.
(The == operator checks whether two objects are stored in the same memory location, rather than whether they contain the same value. Occasionally, for some objects, you do want to make such a check—but rarely for strings.)
Instead, you should compare strings using subroutines such as equals() and compareTo()
Another place where == and != don’t work as you would expect is with Double.NaN, the constant that represents an undefined value of type double.
The values of x == Double.NaN and x != Double.NaN are both defined to be false in all cases, whether or not x is Double.NaN!To test whether a real value x is the undefined value Double.NaN, use the boolean-valued function Double.isNaN(x).
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.