Which is the Best Java Web Framework ?

Java - Things to know before your OCJP exam - Var Args

1. Var Args argument should have data type followed by three dots.

     void method(int... x){}; // perfectly fine
     void method(int.. x){}; // two dots, compile time error

2. Three dots should be consecutive and not separated by even space.

     void method(int..    .x){}; // compile time error

3. We can have space before and after the dots. Java ignores the space before dealing with it.

     void method(int        ...x){}; // perfectly fine
     void method(int...        x){}; // perfectly fine
     void method(int    ...    x){}; // perfectly fine

4. If there is a var args in the method, it should be only one and the last one.

     void method(int x, int... y){}; // perfectly fine
     void method(int... x, int y){}; // compile time error
     void method(int... x,int... y){}; // compile time error

5. Method can be overloaded with a var args method like

     void method(int x ){};
     void method(int... x){};

In this case, first method will have a priority in case of conflict. method(1) will give a call to first method.

6. Internally Java treats var args as array. That is why Java doesn't allow overloaded method to have var args if there is already a method with an array of same type.

     void method(int... x){}; \\ compile time error: duplicate methods.
     void method(int[] x){};  

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.