简体   繁体   中英

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

Question says it all. Some quick code examples of 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM