15.20
Equality Operators
EXPRESSIONS
At run time, the result of the
instanceof
 operator is
true
 if the value of the
RelationalExpression
 is not
null
 and the reference could be cast ( 15.15) to the
ReferenceType
 without raising a
ClassCastException
. Otherwise the result is
false
.
If a cast of the
RelationalExpression
 to the
ReferenceType
 would be rejected
as a compile time error, then the
instanceof
 relational expression likewise pro 
duces a compile time error. In such a situation, the result of the
instanceof
expression could never be
true
.
Consider the example program:
class Point { int x, y; }
class Element { int atomicNumber; }
class Test {
public static void main(String[] args) {
Point p = new Point();
Element e = new Element();
if (e instanceof Point) {
//
compile time error
System.out.println("I get your point!");
p = (Point)e;
//
compile time error
}
}
}
This example results in two compile time errors. The cast
(Point)e
 is incorrect
because no instance of
Element
 or any of its possible subclasses (none are shown
here) could possibly be an instance of any subclass of
Point
. The
instanceof
expression is incorrect for exactly the same reason. If, on the other hand, the class
Point
 were a subclass of
Element
 (an admittedly strange notion in this example):
class Point extends Element { int x, y; }
then the cast would be possible, though it would require a run time check, and the
instanceof
 expression would then be sensible and valid. The cast
(Point)e
would never raise an exception because it would not be executed if the value of
e
could not correctly be cast to type
Point
.
15.20   Equality Operators
The equality operators are syntactically left associative (they group left to right),
but this fact is essentially never useful; for example,
a==b==c
 parses as
(a==b)==c
. The result type of
a==b
 is always
boolean
, and
c
 must therefore be
of type
boolean
 or a compile time error occurs. Thus,
a==b==c
 does
not
 test to
see whether
a
,
b
, and
c
 are all equal.
362






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved