Test-Driven Teaching – 1. Hello World

The first part of this tutorial for beginners is the ordinary ‘Hello World’ example which won’t be tested. Hey why not? Because this simple example is only used to check if the set-up works.

What you will learn in this section:

  • Set-up of a simple Java project
  • IDE: run the application
  • Methods with parameters
  • for loop

The reader will learn how to set-up the example in his integrated development environment (IDE) of choice, build and run it. He will see that the ‘Hello World’ will be printed in the console output window of the IDE.

I will explain the set-up for NetBeans only. But there are other free and very powerful IDEs as well. E.g. Eclipse and IntelliJ Idea can be used as good alternatives.

Set-up

As the very first step download and install the Java Development Kit (JDK) from Sun (not Java Runtime Environment = JRE). Then get the latest NetBeans here where the ‘Java SE’ package is sufficient.

Get the example as zip file here and extract it into your local folder using NetBeans:

Open the favourite window under Window->Favourites. Right click into this window and add the download folder (with the zip file) to favourites:

Expand the zip file (left triangle) and copy the folder via right click:

Then paste it where you like. Open the project via right click->Open Project ‘TestDrivenTeaching1′ or open it via File->Open Project…

All the code is free software and stands under the GPL v3.0. All sources are located under the src folder; all subfolders of this folder are equivalent to Java packages and are used to better organize your code. You will see the logical ‘Java package’-view on this project within the ‘Projects Window’ and the ‘ordinary folder’-view in the ‘Files Window’:

So look into the package de.tdtut.section1 to open the HelloWord class with double click:

Then click on ‘Run File’ or press SHIFT F6 within this HelloWorld file and you will see in the ‘Output Window’ sth. like:

run:

Hello World

0

BUILD SUCCESSFUL

(The first and last statement are generated from NetBeans and should be ignored!)

Congrats for your first Java program!

If you compile the Java sourcecode, which is done by the IDE, it will generate bytecode (several files ending with .class). This bytecode could run on every operating system by the Java interpreter (java) and is automatically bundled to one jar file from your IDE. The Java interpreter creates plattform-specific machine code from the bytecode and extremely optimizes it.

Now more details how the Hello-Word-example works:

Under the hood the character-band (called String) “Hello World” will be passed as the single parameter to the method println (‘print line to standard output’) of the single object called ‘System.out’.

Every standalone Java application can be started outside of NetBeans e.g. from command line. Go to the folder of the project and type:

java -cp dist/TestDrivenTeaching.jar de.tdtut.section1.HelloWord

As output you should see ‘Hello World’ only. The last argument must be a class with the main method:

public static void main(String[] args)

Advanced Information

If you append some arguments on the command line e.g. via:

java -cp dist/TestDrivenTeaching.jar de.tdtut.section1.HelloWord arg1 arg2

You will see

Hello World
2
arg1
arg2

This is because of the for-loop, which looks into the list of strings and picks one after another.


for (String arg : args) {
  System.out.println(arg);
}

This list is called array and was initialized from the command line arguments and has a static lenght, which cannot be changed afterwards. If you want to access one element of an array you can do this via args[0] and args[1]. Be sure that the number (called index) starts from 0 and goes only until args.length – 1. So if you want to have full control of the iteration use this version with an explicit counter of type int:

for (int i = 0; i < args.length; i++) {
  System.out.println(args[i]);
}

The counter i is declared (and visible) only within the block of the for-loop. With the line

int i = 0;

i was declared and initialized with 0. After each step i will be incremented by one. The loop will execute its block only if the counter i is smaller than args.length.

If this part is a bit overwhelming for your head: no problem! Just try a bit around or go ahead to the next section!

Questions

Just try other things to get a deeper knowledge of Java! E.g. you could declare some other variables and print them or try to iterate in inverse order through the array args. The most important step to learn programming or a new programming language is to think what you want and try to implement this.

If you have problems, questions or suggestions, then do not hesitate to comment below!

About these ads

One thought on “Test-Driven Teaching – 1. Hello World

  1. Pingback: Test-Driven Teaching – 2. Initial Box Game « Find Time for the Karussell

Comments are closed.