开发者

What are the Java equivalents of C# 'is' and 'as' keywords?

开发者 https://www.devze.com 2022-12-22 07:13 出处:网络
Question says it all. Some quick code examples of开发者_如何学Go usage would be nice.. thanks!is => instanceof (JLS reference), like this:

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

精彩评论

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