Archive

Posts Tagged ‘java varargs "functional programming"’

Varargs in Java

January 28, 2009 1 comment

I have recently asked most of my fellow coders the following question.

What is the output of this java program?

public class TestClient {
public static void main(String… args) {
System.out.println(“mohan”);
}
}

All of the answers were “Compile time error”. Needless to say, I have introduced varargs to them.

Here is another question, how do you create a list which has 5 pre-defined element in it.

List<Integer> listIntegers = new LinkedList<Integer>();
listIntegers.add(Integer.valueOf(1));
listIntegers.add(Integer.valueOf(2));
listIntegers.add(Integer.valueOf(3));
listIntegers.add(Integer.valueOf(4));

But it is simple with varargs.

List<Integer> ids2 = Arrays.asList(1, 2, 3, 4);

was it cool!!? it is cool if we don’t know Ruby or Lisp.


Advertisements