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 written things 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 the next posts, I will write more about the Java programming language too.

C++ “Hello world”

Ok, the most famous first program on any programming language is the “Hello world” program, so I will explain how to create one in this post.

In my example I will use “g++” in a Linux environment, but “clang++” works exactly the same.

To create a “Hello world” in C++, you need to create an empty file and give it a name with an extension (any name, for example HelloWorld.cpp); “cpp”, “cxx” or “cc” are well-known C++ file name extensions.

The compiler does not verify if the filename is equal to the name of the “class” or anything inside the file; the file can be stored on any folder too, you do not need to create it inside a special folder containing all stuff of a given “package” (à la Java), for example.

So, after creating an empty HelloWorld.cpp, you can open it using any text editor and start to write the following lines of code:

#include <iostream>

int main()
{
  std::cout << "Hello world\n";
}

Save it, open a terminal, go to the folder where your file is located and there, enter this:

g++ HelloWorld.cpp -o HelloWorld

If after entering that command line you do not get any message, BE HAPPY! Your program compiled properly. Otherwise, you did some error in your code and you need to fix it and compile it again.

After compiling it properly, you need to execute the program. In a Linux/Unix environment, you do that writing the name of the program after a ./ :

./HelloWorld

And the program should show:

Hello world

Understanding how all of this worked

The compilation process in C++ has basically three steps:

  1. Passing your program through the "preprocessor"; an entity that performs several text transformations on your code before being compiled.
  2. The actual compilation process, that turns all your code into machine code with several calls to functions that are located in other libraries.
  3. The linking process, that binds the function calls with the actual functions in the libraries the program uses. If you do not specify anything (as in our case), your program will be linked only to the Standard Library that ships with any C++ compiler.

The parts of the program

#include

#include <iostream>

All things that start with "#" are called "preprocessor directives", that are instructions the preprocessor understands and executes.

#include tells to the preprocessor to look for the file named inside quotes or less-than and greater-than and put its content in the place the #include directive was invoked.

If the filename is inside less-than and greater-than characters (as in our case), the preprocessor will look for the file in a previously defined folder the compiler knows about. So, it will look for the file iostream in that folder. In a Linux environment, those files are generally in a path similar to this one (I am using g++ 8.2):

/usr/include/c++/8

If the filename is declared between double quotes, it means the file will be in the current folder or in a folder explictly mentioned while compiling the program.

iostream is the file that contains a lot of code that allows our programs to have data input and output. In our “Hello World”, we will need “std::cout” that is defined in this file.

main function
int main()

When you invoke your program, the operating system needs to know what piece of code it needs to execute. Such piece of code lives inside the function main.

All functions must return something, for example, if you call a function sum that sums two numbers, it must return a value containing the result of the sum. So, the function sum must return an integer value (an int). Some old compilers used to allow the function main() to return “void” (that means: “return nothing”) but the C++ standard specifies the main() function must return an int value.

Anyway, though this function is declared returning an int, if you do not return anything, the compiler does not complain about that and returns a 0. Notice that this behavior is exceptional and it is only allowed for the function main().

The return value of function main() specified if an error occurred or not. 0 means that no error occurred during the program executed; and a non-zero value means that an error occurred. The specific value being returned is completely depending on the programmers design and error mechanisms defined by them.

The program will be executed while the function main() is being executed. When its execution ends, the program automatically ends returning the return value to the Operating System.

The body of any function is declared inside two curly braces.

std::cout
std::cout << "Hello world\n";

std::cout is a pre-existing object that represents the command line output. The “<<" is an operator that basically does, in this case, sends the text "Hello world\n" to the std::cout object, producing an output of such text in the terminal.

The \n character sequence means an end of line.

g++

g++ is the most popular C++ compiler for Unix platforms. These days clang has a lot of popularity and you can replace one to other because clang parameters are completely compatible to the g++ ones.

When you say something like:

g++ HelloWorld.cpp

You are instructing to the g++ compiler to go through all the compilation process for the file “HelloWorld.cpp”. “Go through all the compilation process” in this case means: Running the preprocessor on the file, compiling it, linking it and producing an executable.

Since in this command line in my example above I did not mention the name of the executable file, the g++ command generates a file called “a.out” in the current folder.

To specify the name of the file to be generated, you must invoke g++ with the “-o” option and then the name of the executable file to be generated.