views:

140

answers:

2

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
+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
One thing: when you use operator "as" there is one cast, but in this case there are two casts.
Sergey Teplyakov
@Sergey - there is a good chance that the JIT compiler will optimize the second typecast away.
Stephen C