Simple Java Example

By | October 16, 2018

Java is a popular, general-purpose programming language for a range of tasks including web applications, games development and data analysis. Part of the appeal of Java is that it compiles to java byte code which can be run on any machine with the Java runtime environment installed. This means that developers only need to write once and can let the environment handle differences in platform or architecture. This post shows a simple Java example to show some of the basic concepts of working with the language.

Install Java SDK

To develop with Java you need to install the Java Development Kit, or JDK. The JDK contains the Java runtime environment and the java compiler as well as other java development tools. You can download the JDK here. There are different versions – 1.8 is supported until 2025 so is a good one to go with.

Simple Java Example

To demonstrate the basics of working with Java, here is a simple Java example.

Create a file called ‘HelloWorld.java’ and add these lines of code:

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

We’ll look at the different elements of this file:

The Class Definition

The first line has the class definition. Setting the class as ‘public’ as we have, means that this class can be accessed by any other class– this becomes more relevant as we develop more complex applications. Each java file can only contain a single public class, and that public class needs to have the same name as the ‘.java’ file it is in.

The Main Method

The next statement defines a ‘main’ function for this class. In almost all cases Java applications need a main method to act as the entry point to the rest of the program. If you are developing a package that will only be used as a library for other java applications then a main method will not be needed (the code calling your library will have a main method instead).

The main method is set as public, which means that can be accessed from outside the class. Another common level of access control for Java methods is ‘private’ which means that a method can only be accessed from its own class. One benefit of using private methods is that you do not need to worry about breaking other code which depends on your class when you change a private method.

The main method is also set to be ‘static’ which means that it is a method of the class itself, rather than any particular instance of the class. Making the ‘main’ method static means that Java can run it at run time without needing to create an instance of the class itself.

In Java, the main method needs to be passed a parameter of type String[] , which by convention is assigned to the variable ‘args’. This variable allows external arguments to be passed into the main function, such as command line arguments provided by the user when the program is run.

Printing To Screen

Having done all that setup to get the class and method right, the actual functioning part of our little program is quite simple. We use ‘System.out.println‘ to print ‘Hello, World!’ to the console.

The ‘println’ method is similar to the ‘print’ method, but also puts the cursor to a new line after printing.

Run The Example

Now from the terminal or command line, navigate to the folder containing HelloWorld.java and run

javac HelloWorld.java

This will compile the java code. It should run with no problems.

Compiling the ‘HelloWorld.java’ file will create a new file ‘HelloWorld.class’. You can now test that your code works by running

java HelloWorld

.