1.2 Behind the scenes

In the previous section Get-go we spoke about how to install and run your first Java program—but before we carry onto more interesting things, we first need to know a little about how the code from the previous section works.

In case you’ve forgotten:

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

Code blocks

Code blocks are wrappers for code—defined using {}. Whatever comes inside {} is held together by it, in the form of a unit. These can in turn be attached to various different things, or be placed on their own to serve as different layers inside your code. Code blocks may be placed inside of each other to serve as a deeper part inside your code. The further in a code block is, the deeper into the code’s structure it is. You can see that we’re using two code blocks in the above example, but we’ll come to the meaning of those in a moment. For now, think of them as wrappers of code.

Class

The keyword class on the first line tells Java’s compiler that we’re creating a Java class. A class serves as a module that contains information about itself. Everything in Java requires a class—depending on what type of class information has, it can be used in different ways. We name classes by putting its name after the keyword class—the class from the above example goes by the name of HelloWorld. The output file from compiling our program will have the same name as our class with .java as a file extension.

Let’s have a look at our class:

class HelloWorld
{

}

Beneath the class definition, there’s the start of a code block. We say that this is the class’ body—whatever goes inside a class’ body is contained by that class. The body of any type of thing—class or other, granted it has the possibility of having a body—comes right after its definition. The class HelloWorld contains another interesting structure. Let’s have a look at that.

Main method

Let’s look into our into this part:

public static void main(String[] args)
{

}

The public static void main is the entry point for any Java program—its required that one of such is present in at least one file over a Java project. The public static void main is our main method. A method is a wrapper for code that may be called from somewhere else inside your Java program. The main method in particular is called as soon as Java locates it upon running, which is why you might refer to it as an entry point for you Java program.

The keywords public and static describes the accessibility of this specific method—public makes it accessible from outside its class and static makes it statically accessible, nevertheless you can think of it as required by our compiler in order to locate it. In the same way there’s a code block attached to HelloWorld, there’s a code block attached to main, whereas main is the name of the main method. The name main is required by a main method to have.

Prior to the code block, there are two more things—void and (String[] args)—whereas the keywordvoid tells Java that this is indeed a method. Methods have the ability of having return types—this will be discussed in a future section of this tome—we say that this method has a return type of void. Then there’s also the (String[] args). Whatever comes inside () after the definition of a method, specifies that method’s parameters. Whenever we decide to call a method in the future, we need to provide the method we’re calling with matching information for its parameter blueprint. As for the main method, it contains one parameter—also referred to as an argument—this parameter in particular will be whatever command line arguments the user chooses to run the program with, but don’t worry about that for now.

Inside the main method’s code block—usually called its method body—we put any code that we wish to be executed first whenever we execute—or run, they’re the same—our program. Code blocks can be however big you want them to be. Code inside methods are executed one line at a time from the top to the bottom—meaning that whenever we run this program, main will be went over from top to bottom. When Java reaches the bottom of our main, the program is over and ultimately shuts off.

Print statement

Inside our main method there’s a System.out.println—this is a print statement, and is in fact another method just like that of the main, only now we’re actually calling it ourselves, as well as println already being defined by Java’s standard library. Whatever is located inside a println will print to the console upon being run—since this println contains the text “Hello, world!” inside the two ", our program is going to print “Hello, world!”

By putting text inside two ", we make it into a string containing the text we gave it. Strings are objects—or more simply put, structures of data—able to store text, so that we can use it when calling methods such as println. The println prints out whatever text we decide to provide it with.

When using print statements, it’s entirely up to you to choose what it’ll print:

System.out.println("Hello, mama!");

Go ahead and change its text for yourself a slight bit and see what happens!

Moreover

Don’t worry if there’s anything you don’t yet fully understand, as most of the things covered within this section will be discussed more in-depth in future sections. Sit back and relax—you’ve done well!

Get-go