Week 1

Printing

In the previous lesson, we learned that using the command

System.out.println("Hello World!")

results in the following output to be printed in the console:

Sample output

Hello World!

The same command can be used to print other text as well. For example, to print Goodbye!, we would again use the System.out.println command, but now putting a different value between the brackets. This would look as follows:

System.out.println("Goodbye!")

This leads to the below output being printed to the console.

Sample output

Goodbye!

The text within the brackets of a function (in this case System.out.println) is referred to as an argument. Our first example used "Hello World!" as an argument, while the second used "Goodbye!". But we can use any text, as long as long as it is enclosed in quotation marks "".

Loading

Printing Multiple Lines

Programs are built by adding commands one by one, each on a new line. In the example, there are two instances of the System.out.println command, meaning that the program executes two print statements.

public class Example {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        System.out.println("... and the universe!");
    }
}

The program above will print:

Sample output

Hello world! ... and the universe!

Loading

Code style and Code Comments

Semicolon Separates Commands

Commands are separated with a semicolon ;. We could, if we wanted to, write almost everything on a single line. However, it's not recommended as it makes the code difficult to understand.

System.out.println("Hello "); System.out.println("world"); System.out.println("!");
Sample output

Hello world !

Although the previous example works, it's important to consider readability for other programmers (and your future self!). Using line breaks makes the code easier to read as each line is focused on a single task. This makes it clear what each line is doing.

Comments

Source code can be commented for clarity or to add notes. There are two types of comments:

  • Single-line comments are marked with two slashes //. Everything following them on the same line is interpreted as a comment.
  • Multi-line comments are marked with a slash and an asterisk /*, and closed with an asterisk followed by a slash */. Everything between them is interpreted as a comment.

Below is an example of a program where both are used.

public class Comments {
    public static void main(String[] args) {
        // Printing
        System.out.println("Text to print");
        System.out.println("More text to print!");
        /* Next:
        - more on printing
        - more practice
        - variables
        - ...
        */
        System.out.println("Some other text to print");
        // System.out.println("Trying stuff out")
    }
}

The example's last line highlights a useful case for comments. Code can be commented out instead of being deleted when testing something new. However, it's important to keep the code organized and avoid having old code in comments in the final version of the program.

You have reached the end of this section! Continue to the next section: