While going through various programs recently, I saw following code:
protected Object doInBackground(Object... arg0)
{
....
....
....
}
I dont understand the significance of Object...
I have never seen ...
after any data type.
While going through various programs recently, I saw following code:
protected Object doInBackground(Object... arg0)
{
....
....
....
}
I dont understand the significance of Object...
I have never seen ...
after any data type.
It is called as variable arguments or simply var-args, Introduced in java 5. If you method accepts an var-args as a parameter, you can pass any number of parameters to that method. for instance below method calls would all succeed for your method declaration:
doInBackground(new Object());
doInBackground(new Object(), new Object());
doInBackground(new Object(), new Object(), new Object());
doInBackground(new Object(), new Object(), new Object(), new Object());
A previous post should give you more information Can I pass an array as arguments to a method with variable arguments in Java?
See "Arbitrary Number of Arguments" from http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).
You can input an arbitrary number of Object-parameters to doInBackground
. They are then accessible through the arg0
array in your method. arg0[0]
, arg0[1]
, and so on.
Object is the base type for all classes in Java, except the primitives.
The three dots signify any number of arguments can be passed, aka varargs.
...
allows you to provide multiple arguments called varargs.
doInBackground(1, "hello", "world");
This is how the MessageFormat.format
method works which allows you to specify a format and then objects to fill that format.
public static String format(String pattern,
Object... arguments);
//Usage
MessageFormat.format("hello {0}, I'm from {1}", "John", "Earth");
Its the way we tell that any number of arguments of a specific data type can be sent, rather than writing in 'n' number of times in function def. Check this.
This is actually a variable length or arguments in a method. When you will call this method you may pass n number of objects of Object class to this method and arg0 will be you array ob Objects. It's very old and very simple. Nothing else..
Ellipsis or (...) is known as var-args. In this case, it made the method very generalized method. it can take any number and any type of argument.