Archive

Archive for January, 2009

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

January 13, 2009 Leave a comment

Executing single line of Java code – One Liner in Java.

Use eclipse scrapbook – http://www.eclipsezone.com/eclipse/forums/t61137.html

Another tip for Java-Eclipse :: type “instanceof  ctrl+space” for generate code from code-snippet.

Though both are old, I explored it just now

Categories: Uncategorized