简体   繁体   中英

java: How can I know the type of variable inside of Object?

I have the following code:

void testme(Object a) {
  # printing the type of the variable transferred to the function  
}

how can I know the type of the variable that passed to that function? for example how can I know the difference if the user executed the function as the following:

Integer a=5;
testme(a);

or

String a="a";
testme(a);

in general i'm building a generic functions to work with my database, and I need to use setLong/setInt/setString depends on which type of variable transfered to the function.

any ideas?

thanks

You can use instanceof and/or getClass . The former tests against a specific class, the latter actually gives you the Class object ( String.class , etc.) for the argument. So for instance:

if (a instanceof String) {
    // ...
}
else if (a instanceof Integer) {
    // ...
}
// ...

But for your specific case, you may be able to use one of the versions of PreparedStatement#setObject .

  • object.getClass() returns the class of the object. You can use equals or isAssignableFrom(..) to make reflective comparisons.
  • you can use the instanceof operator to make static comparisons: if (object instanceof String) {..}

While they are fine in your case, you should avoid using these in the general case. If you know in advance what will be the types that are passed, overload the methods: foo(Integer i) and foo(String s) . If you don't know, you can use polymorphism and double-dispatch. Just make your objects implement a common interface:

interface Testable {
   void invokeTest();
}

class FooTest implements Testable {
    public void invokeTest() { // code specific for FooTest }
}

class BarTest implements Testable {
    public void invokeTest() { // code specific for BarTest }
}

And then:

public void test(Testable testable) {
    test.invokeTest();
}

Use the instanceof keyword

if(something instanceof String)
{
}
else if(something instanceof YourType)
{
}

if(一个整数的实例)或if(一个String的实例)

If you're end goal is to operate with the database it's best NOT to use Object on your function like this. It doesn't tell anyone what is legal to pass. I'd suggest using JDBC setObject(int parameterIndex, Object x, int targetSqlType) instead, and have the caller identify the JDBC Type to you:

 public void someFunction( Object obj, int targetSqlType ) {
    statement.setObject( nextIndex, obj, targetSqlType );
 }

What you are doing with instanceof or getClass() is typically a bad practice. You're loosing vital type information by using Object then turning around and hard coding that type information in an if ladder. It's better to define multiple methods for each type all named the same thing. So if you have someFunction that can take 5 different parameter type you'd do the following:

  public void someFunction( Integer value ) {
       statement.setInt( convertThisValue( value ) );
  }

  public void someFunction( Long value ) {
       statement.setLong( convertThisValue( value ) );
  }

  public void someFunction( String value ) {
     ...
  }

  public void someFunction( Boolean value ) {
     ...
  }

  public void someFunction( Double value ) {
     ...
  }

  private <T extends Number> T convertThisValue( T value ) {
     // do some shared logic on processing the value
  }

In this example I've shown how you can easily define all the types you understand as separate methods differing only by type, and how you might centralize some conversion logic in a generic function (convertThisValue()) so you can share some processing between multiple types. What's better about this approach is you're using the Java complier to handle the work for you matching up the right method with the type the caller passed to it. No need for an if ladder, and the compiler will complain to the user when they aren't using a known type as oppose to using Object and having it die at runtime.

In JDBC the known types are well defined so you can easily create methods that know how to handle each type understood by every JDBC database. Also using the setObject() method makes your job much easier.

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