Hello, World and friends.

exercise No. 1

Starting from and extending class Hello

Q:

  1. Fire up you IntelliJ Idea development environment and create the following class Hello:

    void main() {
       IO.println("Hello, World ...");
    }
  2. Run the class and observe the output.

  3. Extend your current code to produce multiple output lines:

    Those who can do.
    Those, who cannot, teach.
    Those, who cannot teach, lecture trainee teachers teaching methodology.

A:

There are several options for creating a whole paragraph spanning multiple text lines:

  1. Using Java 13 multiline string literals (best):

    void main() {
      IO.println("""
         Those who can do.
         Those, who cannot, teach.
         Those, who cannot teach, lecture trainee teachers teaching methodology.""" );
    }
  2. Straightforward: Using multiple println(...) statements:

    void main() {
       IO.println("Those who can do.");
       IO.println("Those, who cannot, teach.");
       IO.println("Those who cannot teach lecture trainee teachers teaching methodology.");
    }
  3. Long strings exceeding your editor's desired maximum line length may be decomposed into smaller chunks using the + ❶ string concatenation operator:

    void main() {
      IO.println(
        "Those who can do.\n"  + ❶
        "Those, who cannot, teach.\n"  +
        "Those, who cannot teach, " +
        "lecture trainee teachers teaching methodology."); // No \n at end, println(...) does it.
    }

    Note

    »\n« represents the newline character. Without it the three sentences would be concatenated into a single line of text.

    The IO.println(...) method adds a final line break to the content in question.