So, What Does This Do?

Let’s examine each of the pieces of what we have been executing, and use those to show you where we will be exploring in this book.

println("hello, world!")

First, we have "hello, world!". This is a Kotlin string. It looks much like strings in Java, Ruby, JavaScript, and many other programming languages. Kotlin strings are a bit more powerful than those in Java, as Kotlin adopted some of the string features offered in other programming languages. We will take a closer look at strings in the next chapter.

Next, we have println(). This is a Kotlin function, one that accepts a string as input and prints it… somewhere. The exact location of where that string gets printed will depend a bit on where Kotlin is running:

Java programmers will wonder where the class and method are. In Java, we do not have standalone lines of code. Rather, all code goes in a class somewhere, usually inside of a method in that class. Usually we import the class and then call the method on that class, such as System.out.println(), which is the Java equivalent of println() in Kotlin. Kotlin supports some statements and functions that are not part of any class, akin to how Ruby and JavaScript can have their own global methods and functions.

Eventually, we wrapped the println() in a function and put it inside a class:

class HelloWorld {
  fun speak() {
    println("hello, world!")
  }
}

This works similarly to how classes and methods are defined in Java and Ruby. JavaScript’s object model is somewhat different but, in the end, you can accomplish much the same thing. We will examine functions in an upcoming chapter and classes a bit after that.

We then created an instance of our class and called our speak() function:

HelloWorld().speak()

Kotlin has very terse syntax for creating an instance of a class: just use the class name like a function. So, HelloWorld() returns an instance of a HelloWorld object. There is no need for a keyword, the way Java, Ruby, or JavaScript uses new.

Calling a function on an object then works much like other object-oriented languages — use . followed by the function call (speak()).

Sometimes, our functions take parameters, as we saw with main():

fun main(args: Array<String>) {
  HelloWorld().speak()
}

Here, args is a parameter, of type Array<String>. Kotlin is a “strongly typed” language: everything in Kotlin is tied to some type. This is similar to Java but distinct from Ruby and JavaScript, which do not use types in this fashion. We will explore parameters and their use of types more in the chapter on functions. We will also look at that angle-bracket syntax — part of Kotlin’s support for generics — in an upcoming chapter.

Over the next several chapters, we will be examining more about these basic building blocks of Kotlin syntax, plus we will be experimenting with more elaborate code snippets.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.