Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Any Type As A Method Parameter [duplicate]

Tags:

java

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?

like image 927
LAB Avatar asked Dec 10 '22 22:12

LAB


1 Answers

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.

like image 156
M A Avatar answered Dec 29 '22 22:12

M A