8

I am trying to have a method that can take in ANY TYPE as a parameter (Object, int, boolean, ArrayList, etc.). Here is a very simple method to illustrate what it would be trying to do:

public void printAnything(Type arg0) {
    System.out.println(arg0);
}

And what I am asking is, what would replace Type in that method?

LAB
  • 85
  • 1
  • 1
  • 6
  • @Trobbins careful. Generics don't support primitive types. – Luiggi Mendoza Jul 01 '15 at 20:33
  • @Luig You're right, thanks for the reminder. – CubeJockey Jul 01 '15 at 20:34
  • 1
    I am not sure, but I think `Object` should do the job, as every object is derived from `Object` and every normal type (int, boolean,...) can get autoboxed, so you should also be able to pass them as arguments... – Byte Commander Jul 01 '15 at 20:34
  • @Byte Commander So, you're saying, I could pass an `Integer` as opposed to an `int`? – LAB Jul 01 '15 at 20:34
  • Yes, of course. And if I remember right, if you can pass an `Integer`, but give an `int`, Java automatically converts it to its object representation, this also works the other way round, depending on what's needed. This is called autoboxing, I think. – Byte Commander Jul 01 '15 at 20:37
  • @LuiggiMendoza in this case it make no difference due to autoboxing. see my answer. – user140547 Jul 01 '15 at 20:50
  • @user140547 primitive and autoboxing may work when using `Object` or generic, but it's not the same. This is noted when you pass an array of primitives and an array of the wrapper class. IMO you should not use any of these approaches for supporting primitive types **ever**. If you want/need to support primitive types, you have to (sadly) overload your method properly. – Luiggi Mendoza Jul 01 '15 at 20:54

4 Answers4

11

In your specific example, Object would fit well because PrintStream#println(Object obj) would print the string representation (using String#valueOf(Object)).

When you pass a primitive value, it will be automatically boxed into its wrapper type, e.g an int would be converted to java.lang.Integer which extends Object.

M A
  • 71,713
  • 13
  • 134
  • 174
4

You can use both

public void printAnything(Object arg0) {
    System.out.println(arg0);
}

and

public void <T> printAnything(T arg0) {
    System.out.println(arg0);
}

Both versions can also be called with primitives thanks to auto-boxing.

see also Java generics T vs Object

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80
2

The data type you need to accept is Object.

Because in Java, everything is derived from it, except the primitive data types like int, float, boolean, etc.
Those however can also be passed if Object is required, because Java automatically converts between primitives (e.g. int) and their object representation (e.g. Integer), depending on which form fits the method prototype. This feature is called autoboxing.

Byte Commander
  • 6,506
  • 6
  • 44
  • 71
1

You can use Object as a Type. It can autobox int to Integer and so on. See here.

Object class is super class for all other. So, you can use it. See also this.

Aleksandar
  • 1,163
  • 22
  • 41