Hello World

5 years ago by in Articles, Introduction Tagged: , , ,

Hello World is the first program found in most programming books (Wiki). Even a book was also written that has Hello World as part of it title (Hello World(s) — From Code to Culture: A 10 Year Celebration of Java Technology Book). We cannot have a website about Java without starting with the Hello World example.

Following is the classic Hello World program.

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

This program is very simple for an experienced developer but it looks cryptic for someone new to Java.

Let’s start decrypting the above code. The first line defines a class named Main. All Java code is found in a class, or better a within a Type. Therefore all things that you need done by your program, will need to be written inside a class (or a Type) like this one. Different from other scripting languages, such as php (Homepage), in Java we cannot have code outside a class.

Right after the class name, Main, there is an opening curly bracket: {. There can be as many spaces as required between the class name and the open curly bracket. The open curly bracket marks the opening of the class content. This is matched by another closing curly bracket which is found at the end of the example. All code is found between these two matching curly brackets.

Note how the code is formatted. The formatting means nothing to Java but ease helps a lot in distinguishing what is inside what. As a rule of thumb, the code is indented whenever a curly bracket is opened.

Inside this class we have a method called main. Methods, or functions as sometimes these are referred to, represents tasks that this class can do. For example the action run (just to clarify run mean running on a track or anywhere you like) can be expressed as a method. This method prints “Hello World” to the command prompt. This is due to the println() method which is defined yet within another class.

The key word public defines the visibility of the class and method. This means that any other Java code can see and access this class and method. The method println(), mentioned before is too public, that is why we can use it from our class. On the other hand should the method be marked as private, then only the class where it is defined will be able to see and access this method. This is like the private room in your house that no-one has access to but you.

This concludes this simple article. Here we simply introduced the most popular class and some key concepts about Java. We’re far from ready and many other interesting articles will follow. Let us know what articles you would like to see here and we will try our best to accommodate you. Feedback is always welcome.

Where to go next?

  1. The article: Getting Started is the next recommended reading. It describes how to get started and how to setup what is required to start programming with Java.
  2. The article: From where to Start, acts as the first steps guide for anyone with no or very little experience.
Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 SearchSecurity.co.UK award (Website).

Leave a Comment


Time limit is exhausted. Please reload the CAPTCHA.