in C# when I want to know if an object is an instance of a particular type or not, I can use "is" operator:
String foo = "hi :)"
if (foo is String) ...
how can I do it in java? (I know I can use try statement, any other way?)
in C# when I want to know if an object is an instance of a particular type or not, I can use "is" operator:
String foo = "hi :)"
if (foo is String) ...
how can I do it in java? (I know I can use try statement, any other way?)
Java equivalent:
String foo = "hi :)"
if (foo instanceof String)
You'd use instanceof
- that's the equivalent of is
in C#. Note that there's no equivalent of as
.
See the JLS section 15.20.2 for more details of instanceof
, but it's basically the same as is
:
// Note: no point in using instanceof if foo is declared to be String!
Object foo = "hello";
if (foo instanceof String)
{
...
}
Try something like this:-
String foo = "hi :)"
if (foo instanceof String)
{
......
}
In java you can use "instanceof" instead of "is"
String foo = "hi :)"
if (foo instanceof String)