Hello, World!

“Hello, world!” is a program commonly used to demonstrate the basic syntax of programming languages.

We’ll develop this program in several steps so you understand all its parts.

First, let’s examine an empty program that does nothing at all:

// HelloWorld/EmptyProgram.kt fun main(args: Array<String>) { // Program code here ... }

The example starts with a comment, which is illuminating text that is ignored by Kotlin. // (two forward slashes) begin a comment that goes to the end of the current line:

// Single-line comment

Kotlin ignores the // and everything after it, until the end of the line. On the following line, it pays attention again.

The first line of each example in this book contains the name of the the subdirectory containing the source-code file—Here, HelloWorld—and the name of the file: EmptyProgram.kt. The example subdirectory for each atom corresponds to the name of that atom.

A keyword is a special word reserved by the language and given special meaning. The keyword fun is short for function. A function is a collection of code that can be executed using that function’s name (we’ll spend a lot of time on functions throughout the book). The function’s name follows the fun keyword, so in this case it’s main.

main is actually a special name for a function; it indicates the “entry point” for a Kotlin program. A Kotlin program can have many functions with many different names, but main is the one that’s automatically called when you execute the program, to get everything started.

The argument list follows the function name and is enclosed by parentheses. Arguments are separated by commas, so in this case we only have a single argument. Arguments are the way you hand pieces of data into the function from outside the function.

An argument name can be anything (with some limitations we’ll discuss in a later atom). In this case the argument name is args, which is conventionally used for main.

You’ll notice that args is followed by a colon. The colon indicates you are about to see the type of that argument—the kind of information the argument holds. In this case the type is Array<String>, which you can say aloud as “array of string.” An Array is a collection of identical elements, and a String is a sequence of characters, like a word, sentence, or paragraph. So an Array<String> is a collection of Strings. That’s all you need to know for now, and we’ll study these items more in future atoms.

After the argument list is the function body, starting with an opening brace ({) and ending with a closing brace (}). The function body consists of one or more statements or expressions. A statement produces an effect, and an expression yields a result.

In this case there are no statements or expressions in the body, just a comment.

You might wonder about the argument list. args is never seen or used again in the program, so why are we bothering with it? Why not just pass nothing at all using an empty argument list: ()? Again, main is special. Here, it requires Array<String> as the only acceptable argument—it’s expecting zero or more command line arguments when the program is invoked. For every other function you define when writing Kotlin, you can choose to have any number or type of arguments.

Now let’s make the program do something: display the phrase “Hello, world!” by adding a line in the main body:

// HelloWorld/HelloWorld.kt fun main(args: Array<String>) { println("Hello, world!") } /* Output: Hello, world! */

The line that displays the greeting starts with println. Like main, println is a function. This shows how you call a function, which means to make it execute its function body. You give the function name, followed by parentheses containing one or more arguments. In this book, when we refer to a function in the prose, we’ll add parentheses after the name as a reminder that it is a function. Here, we say println().

println() takes a single argument, which is a String. A String is defined by putting characters inside quotes.

println() moves the cursor to a new line after displaying its argument, so anything displayed afterwards goes on a separate line. You can use print() instead, which leaves the cursor on the same line.

Unlike some languages, you don’t need a semicolon at the end of an expression in Kotlin. It’s only necessary if you put more than one expression on a single line (this is discouraged).

In some examples in the book, we show the output at the end of the listing, inside a multiline comment. A multiline comment starts with a /* (a forward slash followed by an asterisk) and continues—including line breaks (which we call newlines)—until a */ (an asterisk followed by a forward slash) ends the comment:

/* A multiline comment Doesn't care about newlines */

It’s possible to have code on the same line after the closing */ of a comment, but it’s confusing, so people don’t usually do it.

Comments should add information that isn’t obvious from reading the code. If comments just repeat what the code says, it becomes annoying and people start ignoring your comments. When code changes, programmers often forget to update comments, so it’s good practice to use comments judiciously, mainly for highlighting tricky aspects of your code.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.