Java A First Example

As with most introductory examples to a programming language we will start with the infamous “Hello World” example. This is normally a good way to ensure that your development environment is set up correctly and that you can compile and execute Java code.

1
2
3
4
5
6
7
package programming.javacore.learnjava.hello;

public class HelloWorldCommandLine {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
   }
}

The above code declares a class called HelloWorldCommandLine, and a method called main. We will talk more about classes and methods in a future article, but for now it is enough to understand that all code within a Java program exists in a class and a method is a means for performing a behavior (action). Moreover, the main method within this class is a special method. If a Java class has a method with the signature:

1
public static void main(String[] args)

it is considered a main method and it is an entry point to starting and executing a program. More specifically, it is an entry point for starting a java program on the command line such as from Windows dos or a Linux shell.

In order to run the example we must first compile it. The Java SDK comes with a command line tool called javac this tool can be used to compile the java code into a class file. In order to compile the code on the command line we must first navigate to the source folder.

1
cd   ~/repositories/java/tutorials/java-core/javacore/src/main/java

Next we can use the javac tool to compile the java code.

1
javac programming/javacore/learnjava/hello/HelloWorldCommandLine.java

It is important to note that the since the HelloWorldCommandLine class exists within the package “programming.javacore.learnjava.hello” we must use the package path when compiling as shown above. We will talk more about packages later but at a high level you can think of a package as way of adding a namespace to your code. It is a means of giving your code a unique name in order to distinguish it from other code. It is also a means of conceptualizing your code for example we can order code into different packages within an application, in order to make it easier to locate code with similar function, akin to organizing a file system into files and folders.

To run your java code via the command line we use another tool called java.

1
java -cp /home/stephen/repositories/java/tutorials/java core/javacore/src/main/java programming.javacore.learnjava.hello.HelloWorldCommandLine

The above code executes the HelloWorldCommandLine.class file and prints the “Hello, Java!” on the command line.

A Second Example

1
2
3
4
5
6
7
8
9
public class HelloWorldCommandLineWithArguments {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage:  HelloWorldCommandLineWithArguments <NAME>");
        }

        System.out.println(MessageFormat.format("Hello {0}, Welcome to java", args[0]));
    }
}

The above code is a little different to the first example. In this example we demonstrate how arguments can be passed from the command line, to a Java program.

The main method contains a “args” parameter this parameter receives the parameters passed from the command line. We check that only one parameter has being passed, if not, we output an error on the command line.

If only one parameter is passed we output a message on the command line.

1
2
3
4
java -cp /home/stephen/repositories/java/tutorials/java-core/javacore/src/main/java  programming.javacore.learnjava.hello.HelloWorldCommandLineWithArguments stephen

Outputs:
Hello stephen, Welcome to java

A Third Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.github.senrightlearn.java.helloworld;

import javax.swing.*;
import java.text.MessageFormat;

public class HelloWorldGui {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage:  HelloWorldCommandLineWithArguments <NAME>");
        }

        JFrame frame = new JFrame("Hello, Java!");
        JLabel label = new JLabel(MessageFormat.format("Hello {0}, Welcome to java", args[0]), JLabel.CENTER);
        frame.add(label);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

This example is a little different to the previous two examples. In this example we display the hello message with a graphical user interface. This is achieved through using the java swing libraries.

To run the above example

1
java -cp /home/stephen/repositories/java/tutorials/java core/javacore/src/main/java  programming.javacore.learnjava.hello.HelloWorldGui

Conclusion

In this tutorial we looked at how to create our first program with Java. We looked at three variations of the same hello world java program. The first a simple command line program, the second a command line program that accepts arguments and the third a simple hello world java swing GUI program.