简体   繁体   中英

How can I make a Java function that accepts any type of variable?

I would like to make a function that can accept any incoming variable, regardless of type ( int , double , String or other objects), and then possibly determine the type of variable and act conditionally on the type.

How can I do this?

Overloading is the most recommended option, most of the time you do not need a function that accepts any type of variable.

But what about a function that accepts any Object ? You may need to use instanceof and handle them depending of the data type.

Usage of instanceof : [Name of object instance] instanceof [Name of object type to match]

instanceof returns a boolean : true if and only if type of object instance matches the type to match.

One example of a function or method that accepts "any variable type:"

public static void method(Object obj) {
    if (obj instanceof String)
        System.out.println("I am a String!");

    if (obj instanceof Integer)
        System.out.println("I am an Integer!");

    // Similarly for other types of Object
    if (obj instanceof ... )
        ...

    // The .getClass() is for any Object
    System.out.println(obj.getClass());
}

Please note that making a function that accepts any type of variable is not recommended.

Depending if you want to distinguish boxed values from atomic or not you have the following options, all of which are demoed in the test below:

  1. use Object : you will loose atomic types as they will be boxed
  2. use generics: you will loose atomic types as they will be boxed
  3. use either of the above along with overloading for all atomic types

    Class<?> anyObject(Object val) { return val != null ? val.getClass() : null; } <T> Class<?> anyGeneric(T val) { return val != null ? val.getClass() : null; } @Test public void anyAsObject_alsoViaGenerics() { assertEquals(String.class, anyObject("a string")); assertEquals(String.class, anyGeneric("a string")); // atomic arrays are Ok assertEquals(boolean[].class, anyGeneric(new boolean[]{})); assertEquals(int[].class, anyGeneric(new int[]{})); // atomic: auto-boxed and thus not Ok assertEquals(Boolean.class, anyObject(true)); assertEquals(Boolean.class, anyGeneric(true)); assertEquals(Integer.class, anyObject(125)); assertEquals(Integer.class, anyGeneric(125)); } // with overloading Class<?> any(Object val) { return val != null ? val.getClass() : null; } Class<?> any(boolean val) { return boolean.class; } Class<?> any(int val) { return int.class; } @Test public void any_overloadedForAtomic() { assertEquals(String.class, any("a string")); assertEquals(Boolean.class, any(Boolean.TRUE)); assertEquals(Integer.class, any(Integer.valueOf(125))); assertEquals(boolean[].class, any(new boolean[]{})); assertEquals(int[].class, any(new int[]{})); // atomic assertEquals(boolean.class, any(true)); assertEquals(int.class, any(125)); }

You don't. What you want to do is overload your function and implement each case accordingly. This is the beauty of typed languages and the beauty of object oriented programming.

The best and easiest way is using overload Technique... write your method several times, each time with different signatures... look at this:

public void myFunc(int i)
{
System.out.println(i);
}
public void myFunc(double d)
{
System.out.println(d);
}
public void myFunc(String s)
{
System.out.println(s);
}
public void myFunc(boolean b)
{
System.out.println(b);
}

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