What are the Java equivalents of C# 'is' and 'as' keywords?
Question says it all. Some quick code examples of开发者_如何学Go usage would be nice.. thanks!
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.
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;
精彩评论