Hello World

The first thing you want to see in any language is a Hello World example. In Xtend, that reads as

class HelloWorld {
  def static void main(String[] args) {
    println("Hello World")
  }
}

You see that Xtend looks quite similar to Java. At a first glance the main difference seems to be the def keyword to declare a method. Also like in Java it is mandatory to define a class and a main method as the entry point for an application. Admittedly Hello World programs are not a particular strength of Xtend. The real expressiveness is unleashed when you do real things as you will learn in a second.

An Xtend class resides in a plain Eclipse Java project. As soon as the SDK is installed, Eclipse will automatically translate all the classes to Java source code. By default you will find it in a source folder xtend-gen. The hello world example is translated to the following Java code:

// Generated Java Source Code
import org.eclipse.xtext.xbase.lib.InputOutput;

public class HelloWorld {
  public static void main(final String[] args) {
    InputOutput.<String>println("Hello World");
  }
}

The only surprising fact in the generated Java code may be the referenced library class InputOutput. It is part of the runtime library and a nice utility that is quite handy when used in expressions.

You can put an Xtend class into a source folder of any Java project within Eclipse or any Maven project. If the project is not yet configured properly, Eclipse will complain about the missing library. The xtend.lib has to be on the class path. The IDE will provide a quick fix to add it.

The next thing you might want to do is materializing one of the example projects into your workspace. Right click anywhere in the Navigator view in Eclipse and select New → Example….

In the upcoming dialog you will find two examples for Xtend:


Next Chapter: The Movies Example