What is the easiest way to do 'is' in Java?

Many languages have a facility to check to see if an Object is of a certain type (including parent subclasses), implemented with 'is' and used like this:

if(obj is MyType)

Or slightly more tediously you can in other languages check by using the 'as' keyword to do a soft typecast and seeing if the result null.

I haven't used Java in years and I'm getting back up to speed on it but surely Java has a way to easily do this without delving deep into the Reflection APIs?

Thanks in advance for answers. I have searched both here and other places but the keywords involved are so generic that even though I'm sure this has a simple answer, googling for it is hard.


Asked by: Ada614 | Posted: 28-01-2022






Answer 1

if (objectReference instanceof type){
    //Your code goes here
}

More info here.

Answered by: Brianna565 | Posted: 01-03-2022



Answer 2

You can only use instanceof with a class literal: that is:

Class type = String.class;

if (myObj instanceof String) // will compile

if (myObj instanceof type) //will not compile

The alternative is to use the method Class.isInstance

if (type.isInstance(myObj)) // will compile

Answered by: Brad858 | Posted: 01-03-2022



Answer 3

obj instanceof TargetType returns true just in case TargetType is in the type hierarchy that contains obj.

See Sun's tutorial

Answered by: Owen702 | Posted: 01-03-2022



Similar questions

Typechecking and generics in java generates warnings

I have the following inline Comparator. private static class SampleSorter implements Comparator<SampleClass>{ public int compare(SampleClass o1, SampleClass o2) { if (o1 instanceof Comparable) { return ((Comparable) o1).compareTo(o2); } else if (o2 instanceof Comparable) { return -((Comparable) o2).compareTo(o1); } return 0; } }


typechecking - Efficiently coding the management of parameters with variable types in Java

I am currently implementing a simulation in Java that requires an input of about 30 different parameters. Eventually, I want to be able to read these parameters from a file and also from a GUI, but I am just focusing on the file input for now. My simulation requires parameters that are of different types: Strings, ints and doubles and I currently have them as fields for the simulation e.g. private String si...


Java typechecking error in long "extends" chain

I am a bit baffled about Java type checking: I have a static method declared as: public static void register(EClass eClass, Class<? extends Page<EObject>> pClass) { ... } I call it as: Page.register(HyconmdePackage.Literals.STATION, StationPage.class); where the second argument is declared as (forget about the first one: it's ok): ...


typechecking - Java : Strictly type checking fails?

Suppose I have two classes as follows class A { private Double value; ... //getters and setters } class B { private Double value; ... //getters and setters } Update public static void main(String[] args) { A a = new A(); B b = new B(); a.setValue(b.getValue() != null ? b.getValue() : 0); //works! a.setValue(0); //doesn't work } ...


java - Is it possible to have a 2D array with 2 different datatypes that still takes a {{,},{,}} constructor with typechecking?

In our current code, we have a class called MasterCodeView: public class MasterCodeView implements Serializable { private String code; private String description; //getters and setters } Then, in each of our business objects, we have one or more String[][] attributes: public static final String[][] CONNECTIONMODEDESCRIPTIONS = { ...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top