Java: Hello world

This is one of the simplest Java programs we can write:

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

To be able to compile this program, it needs to be written in a file called MyFirstProgram.java. We need to do that because the Java compiler mandates the class name must be identical to the file where it is being defined.

The sad part here is that our first Java program needs to be written using a lot of advanced constructions that would need to be learnt before using them. Though I will not expand on them in this post, I will explore them in future ones.

So I will describe below what all the things written in the program mean:


public class MyFirstProgram

The reserved word “public” means this class can be accessed from outside this class. Almost all classes are public, but the programmer can also create internal classes (will expand this in other post).

The reserved word “class” is being used here to declare this Java class. A class is the main construction block used by Java programmers to build Java programs. Anything the programmers can implement, needs to be declared inside a class.

MyFirstProgram” is an arbitrary name I decided to use for my class. As you can see, the class names start with uppercase and all “words” that describe the class are concatenated among them and start with uppercase too in order to make easier reading them.

The compiler does not enforce how the class must be named, but, as I mentioned above, it must be identical to the Java file that contains it.

In the code, after “MyFirstProgram” I wrote a “ { “. The ” { ” and ” } ” are block delimiters in Java and they are used to mark respectively the beginning and the end of something. In this case, they mark the beginning and end of my class MyFirstProgram. The Java compiler ignores the spaces, tabs, empty lines and any indentation and formatting made by the programmers that are in fact, only aesthetic constructs that make programs easier to read by humans. The only delimiters the Java compiler understands and enforces are the curly brackets and the semicolons.

So the last “ } ” in the whole program marks the end of the class.

public static void main(String[] args)

All classes can contain zero or more methods. A method is a construction block that contains instructions that will be executed by the Java Virtual Machine (JVM) and that will do something (print a text, compute a value, etc.).

In this case, we are defining a method called “main“.

When we are trying to execute a Java program, we invoke the Java class we want to execute (in my example: MyFirstProgram).

When the Java Virtual Machine is invoked, it looks for a class called (in my example) MyFirstProgram, and when it finds it, it looks for a method called “public static void main(String[] whatever)” inside the class and then it executes all the code that is inside the method.

It must be “public” because the method must be accessed from outside the class in order to be executed.

It must be “static“. “static” is another Java reserved word that marks a method as a “class method” that differs from an “instance method”. A class method belongs to a class and does not need a Java object in order to be invoked. In our case, we do not need anything to start the class, so this method must be “static”.

It must be “void“. “void” is a reserved word that means that the method does something but does not return any result from its execution. In our case, we will print “Hello world” but we will not return anything to the program after that.

It must be “main“. “main” is the name of the method. In other scenarios, we can name our methods whatever we want, but in this case, it must be “main” because the JVM looks for it in order to execute it (as I mentioned above).

The method can receive zero or more parameters. All parameters are described between the “(” after the method name and a “)” that means that the parameter list is finished. In the case of the method main(), it needs to be “String[] args“.

String[]” means “string array” and defines the data type of the parameters “args“. In this case, “args” will contain all parameters that are passed to the method when invoked. Actually “args” can be named with anything (the compiler will not mandate its name), but its type must String array. Starting Java 7, “String[] args” can be also written down as “String... args“.

After declaring the method name and parameters, a ” { ” occurs. It marks the beginning of the method main and the ” } ” that is before the last one, marks the end of the method. All the code that is between those marks, is all the code the JVM will execute.

System.out.println” is the thing that is used to print a text line in the console.

System” is a built-in Java class that contains a lot of methods and attributes (remember, Java classes contains methods and attributes) to provide some system services (access to the console, access to the keyboard, access to the system time, etc.).

out” is something that is inside the System class (“something” actually means “attribute”, discussed in other future blog entry) that represents the output console.

println” is a method that the output console provides us that lets us to print the text line we send as a parameter to the method (in our case: ("Hello world") ).

Notice that after the “)” there is a semicolon. The semicolon is what tells the compiler an instruction ends.

The actual text "Hello world" is in quotes. The quotes tell the compiler that such text is not part of the language and is the actual text string we want to print out.

And that’s it. In next posts I will write more about the Java programming language too.

C++11: C++-style listener list

Consider you want to create a class where you can register a set of listeners to be notified when something occurs. For example, this is a class that performs some task and you want to be notified when the task has been completely executed. See this code in Java:
Continue reading “C++11: C++-style listener list”