Wednesday, February 29, 2012

A student asked me if there's a simple way of making a method that takes a variable number of arguments. In Java, using the ... notation, you can do just that! Let's say, for example, you want to create a method that will add up any number of integer numbers. Using the code below, you can do just that. So simple, so easy, so elegant.

public class AddUpMany { 
  public int addThemAll(int... numbers) { 
    int total = 0; 
    for (int temp : numbers) { 
      total += temp; 
    return total; 

  public static void main(String args[]) { 
    AddUpMany instance = new AddUpMany(); 
    System.out.println("Adding up 5 and 6 gives you " + instance.addThemAll(5,6));      
    System.out.println("Adding up 5,5 and 2 gives you " + instance.addThemAll(5,5,2));
    System.out.println("Adding up 1,2 and 3 gives you " + instance.addThemAll(1,2,3)); 

Saturday, February 25, 2012

Javascript Lessons

It's so often these days that I get asked questions around Javascript, that I felt I should find a resource for my students. After digging around a bit, I found Code Academy, a brilliant site that offers a free Javascript course.

Why Javascript if this site is about Java? Well, some students have started to write all sorts of interesting applications as projects, especially in the mobile space. With smart-phones all the rage these days, and, admit it, some of these devices are incredible, more and more students are interested in developing for them. By using Java as the back-end, they write server applications with a HTML / Javascript interface.

If you are interesting in learning Javascript for free, I'd recommend you give the Code Academy guys a try, it's a great site, with really easy-to-follow lessons that will get you up to speed in just a couple of hours.