Showing posts with label varargs. Show all posts
Showing posts with label varargs. Show all posts

Monday, 27 June 2011

Simplified varargs method invocation


This was a very tricky one to investigate. It’s still not clear what ‘simplified varargs method invocation’ is referring to but one difference (improvement) I was able to narrow down was that of a more specific and helpful warning that jdk7 adds to certain varargs code as below.


import java.util.Collections;
import java.util.List;
import java.util.Map;

public class BetterVarargsWarnings {

static <T> List<T> foo(T... elements) {
return null;
}

static List<Map<String, String>> bar() {
Map<String, String> m = Collections.singletonMap("a", "b");
return foo(m, m, m);
}

}

Thursday, 3 March 2011

Varargs in Java: Variable argument method in Java 5

I think I am late for writing about varargs. The feature of variable argument has been added in Java 5 since its launch. Still I will write something about varargs.
varargs has been implemented in many languages such as C, C++ etc. These functionality enables to write methods/functions which takes variable length of arguments. For example the popular printf() method in C. We can call printf() method with multiple arguments.

1
2
printf("%s", 50);
printf("%d %s %s", 250, "Hello", "World");

 

Syntax

Varargs was added in Java 5 and the syntax includes three dots (also called ellipses). Following is the syntax of vararg method for method called testVar :

1
public void testVar(int count, String... vargs) { }
 
Notice the dots … in above code. That mark the last argument of the method as variable argument. Also the vararg must be the last argument in the method.
Now let us check simple hello world varargs code.
 
Example:
public class HelloWorldVarargs {

public static void main(String args[]) {
test(215, "India", "Delhi");
test(147, "United States", "New York", "California");
}

public static void test(int some, String... args) {
System.out.print("\n" + some);
for(String arg: args) {
System.out.print(", " + arg);
}
}
}




In above code, the test() method is taking variable arguments and is being called from main method with number of arguments.


So when should you use varargs?

As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.

Saturday, 12 December 2009

Functions or Methods in java

Class behaviour are represented in Java by methods. To declare a method use the following syntax:
[ "public" | "private" | "protected" ] [ "final" ]
[ "static" | "abstract" | "native" ]
return_data_type method_name "(" parameter_list ")"
"{"
// some defining actions
"}"
Accessibility keywords are the same as for properties. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
 
public static void example1() {}
public static int add2(int x) {x+=2; return x;}
public static double example3(int x, double d) {return x*d;}
public static void example4(int x, int y, boolean flag) {}
public static void example5(int arr[]) {} // note: this is object

Note: Differences from cpp 
Types of methods
Pass by value or reference