Files
kotlin-fork/docs/confluence.jetbrains.com/Kotlin/40701077_Hello%2C+world%21.confluence
T
2012-05-30 14:41:40 +04:00

104 lines
3.3 KiB
Plaintext

This page gives some very basic examples of [Kotlin] syntax.
h3. Simplest version
{jet:linenumbers=true}
package demo
fun main(args : Array<String>) {
println("Hello, world!")
}
{jet}
Line 1 is the optional [package header|Packages#Header package declaration].
Then we declare a [package-level|Packages] function {{main}} which [returns {{Unit}}|Functions#Unit] and takes an [Array|Basic types#Arrays] of strings as a parameter.
Note that [semicolons are optional|Grammar#Semicolons].
h3. Reading a name from the command line
{jet:linenumbers=true}
fun main(args : Array<String>) {
if (args.size == 0) {
println("Please provide a name as a command-line argument")
return
}
println("Hello, ${args[0]}!")
}
{jet}
Line 6 demonstrates [string templates|Strings#Templates] and [array access|Basic types#Arrays].
h3. Reading many names from the command line
{jet:linenumbers=true}
fun main(args : Array<String>) {
for (name in args)
println("Hello, $name!")
}
{jet}
Line 2 demonstrates the [*for*\-loop|Control structures#For loop], that would have been called "enhanced" if there were any other *for*\-loop in [Kotlin] :-)
h3. An "object-oriented" Hello
{jet:linenumbers=true}
class Greeter(val name : String) {
fun greet() {
println("Hello, ${name}");
}
}
fun main(args : Array<String>) {
Greeter(args[0]).greet()
}
{jet}
Here we have a [class|Classes and Inheritance] with a _primary constructor_ and a member function.
Note that there's no *new* keyword used to create an object.
h3. A multi-language Hello
{jet:linenumbers=true}
fun main(args : Array<String>) {
val language = if (args.size == 0) "EN" else args[0]
println(when (language) {
"EN" -> "Hello!"
"ES" -> "¡Hola!"
"RU" -> "Привет!"
else -> "Sorry, I can't greet you in $language yet"
})
}
{jet}
In this example, *val* denotes a declaration of a read-only local variable, that is assigned an [*if*\-expression|Control structures#If expression]. Then we use very basic [pattern matching|Pattern matching] expression.
h3. Extension function {{hello()}}
{jet:linenumbers=true}
fun String.hello() {
println("Hello, $this!")
}
fun main(args : Array<String>) {
"world".hello() // prints 'Hello, world!'
}
{jet}
In this snippet, we define an [extension function|Extension functions] {{hello()}} for the type {{String}} that uses *this* in a [string template|Strings#Templates]. And then we call this function on a string {{"world"}}, i.e. we pass this string as the receiver-argument (*this*\-argument) to {{hello()}}.
h3. LINQ-like Hello
{jet:linenumbers=true}
fun main(args : Array<String>) {
args filter {it.length() > 0} forEach {print("Hello, $it!")}
}
{jet}
Here we first called the {{filter()}} [higher-order function|Functions#Higher-order functions] to select only those string from the {{args}} array that have non-zero length. To do this, we passed a [function literal|Function literals] that checks the length of its parameter. the name {{it}} is the default name of a parameter for a function literal.
Filter created a collection for us, and we called {{foreach()}} on that collection, passing in a function literal to be executed for each element. And that function literal just prints a greeting with the passed string.
h3. What's next
* [Basic syntax walk-through|Basic syntax walk-through]