简体   繁体   中英

Java: how to “instanceof argument”

I have tried:

package ro.ex;    

import java.io.IOException;
import java.lang.reflect.Type;

class Ex {
    public boolean isIns(Object o, Class t) {
        o instanceof t;
    }
    public static void main(String[] args) throws IOException {}
}

above code will raise unknown class "t"

My question is: How to pass above code.

update:

following code can't pass intellij idea syntax checker

public boolean isIns(Object o, Class<?> t) {
    return o instanceof t;
}

so the right code in idea is:

public boolean isIns(Object o, Class<?> t) {
    return t.isAssignableFrom(o.getClass());
}

the more simple way is:

package ro.ex;

import java.io.IOException;
import java.lang.reflect.Type;

class Ex {
    public boolean isIns(Object o, Class t) {
        return t.isInstance(o);
    }

    public static void main(String[] args) throws IOException {
        Object r = new Ex().isIns("", String.class);
        System.out.println(r + "\t\t" + new Exception().getStackTrace()[0].getFileName() + ":" + new Exception().getStackTrace()[0].getLineNumber());
    }
}

If you write x instanceof t , then t must be a class. In your isIns method, t is not a class, it is a variable of type Class .

The class Class , however, does offer methods with which you can decide whether some other class is a subclass of it: Class.isAssignableFrom(Class) . So you can change you method to:

public boolean isIns(Object o, Class t)
{
    return t.isAssignableFrom(o.getClass());
}

(I also changed your code so that the result of is returned to the caller.)

I have no idea what you are trying to do with your method, but the instanceof syntax is wrong.

 public boolean isIns(Object o, Class t) {
       return  o instanceof t;
    }

instanceof keyword checks with a Valid class name not with variable name. That's the compiler error.

For ex : o instanceof String , if you write like below, it won't compile

public boolean isIns(Object o, String str) {
        return o instanceof str;  //err, o instance of String is correct way to check.
    }

And I slightly changed your method signature to match the statement.

In your code below, t should be the name of a Java class (eg String ). In your code, you've passed a variable name which is not appropriate:

public void isIns(Object o, Class t) {
    o instanceof t;
}

Since an instanceof check is a one-liner, I'm not sure why you are wrapping it in a method. But if you insist on doing so, perhaps this is what you want:

public static boolean isIns(Object o, Class<?> t) {
  return c.isInstance(o);
}

But two notes:

  1. Lots of instanceof checks generally indicate bad design.

  2. A Java programmer will be much more comfortable seeing instanceof rather than isIns(...) .

You should use the dinamic equivalent of instanceof from Class class. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance(java.lang.Object)

  public void isIns(Object o, Class t) {
        t.isInstance(o);
  }

Check for nulls if needed.

maybe you want something like this?

public class test2 {

    class c {
    }

    public test2() {
        c obj = new c();
        System.out.println(isIns(obj));
    }

    public boolean isIns(Object o) {
        return (o instanceof c);
    }

    public static void main(String argv[]) {
        new test2();
    }
}

EDIT: Removed senceless Class parameters.

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