Monday, June 16, 2014

Varargs | Variable Argument - javatpoint



http://www.informit.com/articles/article.aspx?p=2861454&seqNum=7
Recall from Item 28 that a non-reifiable type is one whose runtime representation has less information than its compile-time representation, and that nearly all generic and parameterized types are non-reifiable. If a method declares its varargs parameter to be of a non-reifiable type, the compiler generates a warning on the declaration. If the method is invoked on varargs parameters whose inferred type is non-reifiable, the compiler generates a warning on the invocation too. The warnings look something like this:
warning: [unchecked] Possible heap pollution from
    parameterized vararg type List<String>
Heap pollution occurs when a variable of a parameterized type refers to an object that is not of that type [JLS, 4.12.2]. It can cause the compiler’s automatically generated casts to fail, violating the fundamental guarantee of the generic type system.
https://www.geeksforgeeks.org/method-overloading-ambiguity-varargs-java/
Another example of ambiguity:
The following overloaded versions of fun( )are inherently ambiguous:
static void fun(int ... a) { // method body  }
static void fun(int n, int ... a) { //method body }
Here, although the parameter lists of fun( ) differ, there is no way for the compiler to resolve the following call:
fun(1)
This call may resolve to fun(int … a) or fun(int n, int … a) method, thus creating ambiguity. To solve these ambiguity errors like above, we will need to forego overloading and simply use two different method names.
https://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50
  public void operation(String str, Integer... data) { 
 String signature = "(String, Integer[])"; 
 out.println(str + "= > " + signature); } 
  
  public void operation(String str, int... data) { 
 String signature = "(String, int[])"; 
 out.println(str + "= > " + signature); } 

  public static void main(String[] args) {
 Program ref = new Program();
 ref.operation("(String, int)", 1); //compile error

  }
Example I does not compile. The error is "The method operation(String, Integer[]) is ambiguous for the type Program".
At compile time a vararg is converted to an array. In example I, 1 will be converted to an int array with 1 as the only element, something like int[] a={1}; with the autoboxing, 1 can also be converted to an Integer array, something like Integer [] b={1}; so if the Program class had only one operation() method, either method would work fine.

Varargs | Variable Argument - javatpoint
Advantage of Varargs:
We don't have to provide overloaded methods so less code.

http://javarevisited.blogspot.com/2011/09/variable-argument-in-java5-varargs.html
1) Every call to varargs method require an anonymous array to be created and initialized which could affect performance in time critical application. There is an alternative of varargs method to achieve better performance. suppose you have a variable argument method sum(int... num) and its called with 2 parameters on 90% of time. In order to avoid array creation and initialization you can use method overloading in Java to provide two versions ofsum() which accept int instead of varargs. here is an example of better performance alternative of varargs for 90% of time

public int sum(int a);
public int sum(int a, int b);
public int sum(int... num);

Now 90% of time method without varargs will be invoked and 10% of time method with variable argument will be invoked.

2) An example of variable argument method from JDK is Arrays.asList(T... args) which was used to convert array to ArrayList before JDK 1.5 but retrofitted to support variable argument in JDK 1.5. Now you can also invoke this method by just passing as many Strings or object as you want and creating a List representation on the fly. Its one of the quickest way to convert Strings into List e.g.

List listOfString = Arrays.asList("Red", "White", "Blue");


Read more: http://javarevisited.blogspot.com/2011/09/variable-argument-in-java5-varargs.html#ixzz4OS6uwkPd

Read full article from Variable argument or Varargs methods from Java 5 with Example - Programming Tutorial

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts