-1

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.

Sharda Singh
  • 727
  • 3
  • 10
  • 19
  • This is for Variable length argument. you can pass one argument ,two argument or as much you like. and in the method these can be get as array Of objects. http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html – Nirbhay Mishra Apr 03 '13 at 10:56

8 Answers8

6

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?

Community
  • 1
  • 1
PermGenError
  • 45,977
  • 8
  • 87
  • 106
3

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.

Sami N
  • 1,170
  • 2
  • 9
  • 21
0

You can think of Object as a parent of all the runtime Instances of userdefined classes in java. The "..." refers to Varargs in java. Here is an example of varargs. It will be essentially interpreted as Arrays.

codeMan
  • 5,730
  • 3
  • 27
  • 51
0

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.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

... 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");
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
0

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.

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
0

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..

Saurabh Agarwal
  • 323
  • 2
  • 6
  • 16
0

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.

Ankit
  • 6,554
  • 6
  • 49
  • 71