Chapter 1 |
Explanation of the first program
public class HelloWorld {
This is the first line of our Java program - class definition. Every Java application must have at least one class definition that consists of
class
keyword followed by a class name. The class name can be anything - in this application, the class is called HelloWorld. By convention, class names start with a capital letter.
Also a class definition contains an access modifier. An access modifier determines how other parts of the program can access it. If a class is
public
, then it can be accessed by other objects. (The opposite of public
is private
which restricts access to the class members.)
Remember that a Java file can have any number of classes but it can have only one public class and the file name should be same as the public class name.
Finally, any class definition contains curly braces
{ ... }
; the rest of the program is written between the braces.
public static void main(String[] args) {
This line is the header of a method. The exact meaning of each word in the method header cannot be given now (since it involves a detailed understanding of several Java features), but a brief explanation is as follows.
public
: this makes the main
method public - that means that we can call the method from outside the class.
static
: we do not need to create objects for static methods to run - static methods can run themselves.
void
: this keyword shows the compiler that the method does not return anything (we hope that you remember from the Computer Programming course that some methods return a value and some do not).
main
: it is the method name. All Java applications begin execution by calling the method main( )
- it is the entry point method from which the JVM can run your program. However, user-defined methods can be created in a similar way (PS: user-defined methods cannot be called main
- this method name is already reserved).
(String[] args)
: this is method's parameter with a data type (String
is a data type and args
is a parameter name). In this example, the parameter is used for command line arguments passed as strings in an array. We will cover this in details in a few weeks.
The last character of the line is a curly brace
{
. This indicated the beginning of the method main( )
body (in Python, we used indentation to specify the beginning and the end of a method). We also hope that you remember from the Computer Programming course that it is quite common that one block of the program is defined inside the other (e.g. conditional statements inside a loop) - consequently, there might be a bunch of opening braces in the code. It is important to double check and place the closing braces on the corresponding lines.
In our first program, the largest block is the class itself. The opening brace is on the first line and the closing brace is on the last line. The pair encloses the body of the class. The second pair of braces encloses the main method.
Remember: all the code we want to run must be placed between these braces.
System.out.println("Hello, World!");
This line is a part of
main
method. It prints the contents inside the double quotes into the console and inserts a newline after. Actually, the output is accomplished by the built-in println( )
method. In this case, println( )
displays the string that is passed to it. The other keywords of the line is a bit complicated to explain in detail at this time. Briefly: System
is a predefined class that provides access to the system, and out
is the output stream that is connected to the console. Thus, System.out
is an object that encapsulates console output.
Also note that the
println( )
statement ends with a semicolon known as terminator. All statements in Java end with a semicolon (except headers of classes and methods).
Comments are not executable statements - they are ignored by the compiler. Nevertheless, comments provide useful explanation and documentation to you and others. It is recommended to add comments:
- at the top of any Java class to describe the overall function of the class
- at the top of any method to describe the overall function of the method
- to a particularly complicated part of the code
- to deactivate some lines of code while debugging (no need to delete the code :)
There are two kinds of comments:
- multi-line comment: begins with
/*
and ends with*/
, and may span more than one line - end-of-line (single-line) comment: begins with
//
and lasts until the end of the current line
Useful link: there is a third type of comments which are used for generating Java code documentation in HTML format from Java source code. The documentation is generated using the comments with the following appearance:
/** * This is a documentation comment */ /** * This is also a documentation comment * @author Mari Tamm * @version 1.0 * @since 2019-01-31 */
More information about Javadoc can be found here.
Self-assessment
Answer a self-assessment question to check if you have understood the video:
Chapter 1 |