Skip to main content

Using varargs(Variable -length argument) in Java

Sometimes you might want to write a method that takes a variable number of arguments. In Java, there is a feature called variable-length arguments, or varargs for short, which allows you to do this. Prior to JDK 5, variable-length arguments could be handled in two ways,


  1. using overloading
  2. using array argument.


syntax for varargs


public static String format(String pattern,
                                Object... arguments);


  • The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. 
  • Varargs can be used only in the final argument position
  • There can be only one variable argument in a method.
  • Vararg Methods can also be overloaded but overloading may lead to ambiguity.
Example: 

public class Varargs {

 public static void main(String[] args) {
  printItems(1,2, "mango", "apple", "orange");
  
  printItems(2,1, "book", "pen", "pencil", "cheese");
 }
 
 public static void printItems(int a, int b, String... items) {
  System.out.println(" --------- Shopping items ----------\n");
  
  for(int i = 0; i < items.length; i++) {
   System.out.println(items[i]);
  }
 }
}
Output :

 --------- Shopping items ----------

mango
apple
orange
 --------- Shopping items ----------

book
pen
pencil
cheese

Comments

Popular posts from this blog

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca...

Java Increment Operations: `n++` vs `n = n + 1` vs `n += 1`

In Java, incrementing a variable by one can be done in several ways: n++ , n = n + 1 , and n += 1 . While these expressions achieve the same end result, they differ slightly in syntax and use cases. Let's explore each one and discuss their performance. 1. n++ Post-Increment Operator : Increments the value of n by 1 after its current value has been used. Common Usage : Typically used in loops and other contexts where the current value needs to be used before incrementing. int n = 5; n++; // n is now 6 2. n = n + 1 Addition Assignment : Explicitly sets n to its current value plus 1. Readability : Straightforward and clear, though slightly more verbose. int n = 5; n = n + 1; // n is now 6 3. n += 1 Compound Assignment Operator : Equivalent to n = n + 1 , but more concise. Usage : Combines addition and assignment into one step. int n = 5; n += 1; // n is...

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package ...