Contents
Section 36.18: Dierent Kinds of Streams #5 - lazily iterate a list of strings, map the values, convert to Int, find max ............................................................................................................................................................... 77
You may also like ........................................................................................................................................................ 88
About
Please feel free to share this PDF with anyone for free, latest version of this book can be downloaded from: https://goalkicker.com/KotlinBook
This Kotlin Notes for Professionals book is compiled from Stack Overflow Documentation , the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not affiliated with official Kotlin group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective
company owners
The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk
Please send feedback and corrections to web@petercv.com
Chapter 1: Getting started with Kotlin
Version Release Date
1.0.0 2016-02-15
1.0.1 2016-03-16
1.0.2 2016-05-13
1.0.3 2016-06-30
1.0.4 2016-09-22
1.0.5 2016-11-08
1.0.6 2016-12-27
1.1.0 2017-03-01
1.1.1 2017-03-14
1.1.2 2017-04-25
1.1.3 2017-06-23
1.1.6 2017-11-13
1.2.2 2018-01-17
1.2.3 2018-03-01
1.2.4 2018-04-19
Section 1.1: Hello World
All Kotlin programs start at the main function. Here is an example of a simple Kotlin "Hello World" program:
package my.program
fun main( args: Array< String>) {
println( "Hello, world!" )
}
Place the above code into a file named Main.kt (this filename is entirely arbitrary)
When targeting the JVM, the function will be compiled as a static method in a class with a name derived from the filename. In the above example, the main class to run would be my.program .MainKt .
To change the name of the class that contains top-level functions for a particular file, place the following annotation at the top of the file above the package statement:
@file: JvmName( "MyApp" )
In this example, the main class to run would now be my.program .MyApp .
See also: Package level functions including @JvmName annotation. Annotation use-site targets
Section 1.2: Hello World using a Companion Object
Similar to using an Object Declaration, you can define the main function of a Kotlin program using a Companion Object of a class.
package my.program
class App {
companion object {
@JvmStatic fun main( args: Array< String>) { println( "Hello World" )
}
}
}
The class name that you will run is the name of your class, in this case is my.program .App .
The advantage to this method over a top-level function is that the class name to run is more self-evident, and any other functions you add are scoped into the class App. This is similar to the Object Declaration example, other than you are in control of instantiating any classes to do further work.
A slight variation that instantiates the class to do the actual "hello":
class App {
companion object {
@JvmStatic
fun main( args: Array< String>) { App() .run ()
}
}
fun run() {
println( "Hello World" )
}
}
See also: Static Methods including the @JvmStatic annotation
Section 1.3: Hello World using an Object Declaration
You can alternatively use an Object Declaration that contains the main function for a Kotlin program.
package my.program
object App {
@JvmStatic fun main( args: Array< String>) {
println( "Hello World" )
}
}
The class name that you will run is the name of your object, in this case is my.program .App .
The advantage to this method over a top-level function is that the class name to run is more self-evident, and any other functions you add are scoped into the class App. You then also have a singleton instance of App to store state and do other work.
See also: Static Methods including the @JvmStatic annotation
Section 1.4: Main methods using varargs
All of these main method styles can also be used with varargs :
package my.program
fun main( vararg args: String ) {
println( "Hello, world!" )
}
Section 1.5: Compile and Run Kotlin Code in Command Line
As java provide two different commands to compile and run Java code. Same as Kotlin also provide you different commands.
javac to compile java files. java to run java files.
Same as kotlinc to compile kotlin files kotlin to run kotlin files.
Section 1.6: Reading input from Command Line
The arguments passed from the console can be received in the Kotlin program and it can be used as an input. You can pass N (1 2 3 and so on) numbers of arguments from the command prompt.
A simple example of a command-line argument in Kotlin.
fun main( args: Array< String>) {
println( "Enter Two number" )
var ( a, b) = readLine()!! .split ( ' ' )
// !! this operator use for NPE(NullPointerException). println( "Max number is : ${maxNum(a.toInt(), b.toInt())}" )
}
fun maxNum( a:
Int , b:
Int ):
Int {
var max = if ( a > b) {
println( "The value of a is $a" ); a
} else {
println( "The value of b is $b" ) b
}
return max;
}
Here, Enter two number from the command line to find the maximum number. Output:
Enter Two number
71 89 // Enter two number from command line
The value of b is 89 Max number is: 89
For !! Operator Please check Null Safety . Note: Above example compile and run on Intellij.
Chapter 2: Basics of Kotlin
This topic covers the basics of Kotlin for beginners.
Section 2.1: Basic examples
1. The Unit return type declaration is optional for functions. The following codes are equivalent.
fun printHello( name: String ?):
Unit {
if ( name !=
null )
println( "Hello ${name}" )
}
fun printHello( name: String ?) {
...
}
2. Single-Expression functions:When a function returns a single expression, the curly braces can be omitted and the body is specified after = symbol
fun double( x:
Int ):
Int = x * 2
Explicitly declaring the return type is optional when this can be inferred by the compiler
fun double( x:
Int ) = x * 2