What is Variable-Length Arguments in Java Language?



Variable-Length Arguments is Java Language




  • J2SE 7 added new Feature that simplifies creation of methods that need to take a Variable number of arguments. This Feature is known as Varargs short for Variable-Length Arguments.
  • In some situations require that variable number of arguments passed to method are not unique.

________________________________________________________


Example:

A method for opening internet connection take this information:
  • User name.
  • Password
  • Filename
  • Protocol
  • etc etc
If some information is not supplied by supply defaults.


___________________________________________________________________


A variable-length arguments is specified by three term.

For example:


How vaTest()//(Method) is written using vararg.


v is implicitly declare as array of int type.


Inside vaTest() v is accessed by using normal array syntax.

___________________________________________________________________


Declaration:

int Any(int a, int b. int c, int...vals){}


Incorrect declaration:

int Any(int a, int b. int c, int...vals, boolean stopFlag){// show error}

___________________________________________________________________

Declaration is correct in which must be only One varargs Parameter.

___________________________________________________________________

Error:


int Any(int a , int b, int vals,int... moreVals){// show error}

___________________________________________________________________

 



Comments

Popular posts from this blog

Over View of Java

Type Casting or Conversion in Java

Java