Hello, World and friends.

exercise No. 1

Extending class HelloWorld

Q:

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

    public class HelloWorld {
    
       public static void main(String[] args) {
          System.out.println("Hello, World ...");
       }
    }
  2. Run the class and observe the output.

    As we will see later you actually run (or execute) the classes main(...) method rather than the class itself. Actually a class in Java cannot be run or executed.

  3. Extend your current code for producing 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 (easy):

    public static void main( String[] args ) {
      System.out.println("""
        Those who can do.
        Those, who cannot, teach.
        Those, who cannot teach, lecture trainee teachers teaching methodology.""" );
    }
  2. Straightforward: Using multiple print statements:

    public class HelloWorld {
    
     public static void main(String[] args) {
        System.out.println("Those who can do.");
        System.out.println("Those, who cannot, teach.");
        System.out.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:

    public static void main(String[] args) {
      System.out.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 long line.

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