开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜