Question says it all. Some quick code examples of usage would be nice.. thanks!
+8
A:
is
=> instanceof
(JLS reference), like this:
Object foo = "hello";
if (foo instanceof String) {
// Yup, it's a string
}
There's no equivalent of C#'s as
operator in Java.
Jon Skeet
2010-03-10 14:44:28
+2
A:
is
(C#) -> instanceof
(Java)
And you get no direct equivalent of as
. You could try this one-liner though:
SomeParentType obj =
original instanceof Child ? (SomeParentType)original : null;
Justin Niessner
2010-03-10 14:45:10
One thing: when you use operator "as" there is one cast, but in this case there are two casts.
Sergey Teplyakov
2010-03-10 14:52:19
@Sergey - there is a good chance that the JIT compiler will optimize the second typecast away.
Stephen C
2010-03-10 14:54:03