---
title: Kotlinことはじめ
tags: ["Kotlin"]
categories: ["Programming", "Java", "Kotlin"]
date: 2014-06-28T20:06:55Z
updated: 2014-06-28T20:06:55Z
---

Better Javaとして[Kotlin](http://kotlin.jetbrains.org/)やってみよう。JavaScriptもいけるらしいから。

とりあえずMavenではじめる。


    $ mvn -B archetype:generate -DgroupId=hello -DartifactId=hello-kotlin -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart

pom.xmlを以下のように修正

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>hello</groupId>
        <artifactId>hello-kotlin</artifactId>
        <packaging>jar</packaging>
        <version>1.0.0-SNAPSHOT</version>
        <name>hello-kotlin</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <version>${kotlin.version}</version>
                    <configuration/>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>process-test-sources</phase>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <kotlin.version>0.7.270</kotlin.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    </project>

バージョンはIntelliJ IDEのKotlin Pluginのバージョンと合わせておかないとエラーが出た・・・（新しいの使うとエラーが出た）

`kotlin-stdlib`と`kotlin-maven-plugin`を設定しておけばいい感じか？


src/main/java/hello/hello.ktを作成

    package hello
    
    fun main(args: Array<String>) {
        println("Hello World!");
    }

KotlinとJavaを同居したいならsrc/main/java以下に置くので良さげ。コンパイル`mvn compile`すると

`hello/HelloPackage.class`ができていることがわかる。とりあえずMavenのexecプラグインで実行

    $ mvn -q exec:java -Dexec.mainClass=hello.HelloPackage
    Hello World!

できた。IDEA上で実行した方が楽だけど・・・

とりあえずFizzBuzz

    package fizzbuz
    
    fun main(args: Array<String>) {
        IntRange(1, 100).map { x ->
            when {
                x % 15 == 0 -> "FizzBuzz"
                x % 5 == 0 -> "Buzz"
                x % 3 == 0 -> "Fizz"
                else -> x
            }
        }.forEach { x -> println(x) }
    }

when式の分、[Java8版](/#/entries/266)より書きやすいですね。

**追記**: もうちょい短くかけた。いいねKotlin.

    fun main(args: Array<String>) {
        (1..100) .map { x ->
            when {
                x % 15 == 0 -> "FizzBuzz"
                x % 5 == 0 -> "Buzz"
                x % 3 == 0 -> "Fizz"
                else -> x
            }
        }.forEach { println(it) }
    }

[デモ](http://kotlin-demo.jetbrains.com/)で一通り勉強できそう。JavaScript気になる
