You should have written the code in Kotlin last year! I looked at the document, but when I thought that I didn't recognize it, I wrote Go last year ... I looked at Kotlin because it was a boat I got on.
"Getting Started with IntelliJ IDEA" and IntelliJ appear first in Kotlin's tutorial Getting Started, so is it popular? Download to install the community version.
IntelliJ feels cool just because it's darker than when I saw it before. Until you create a new project, add a Kotlin file and run it, it will be crisp as "Getting Started with IntelliJ IDEA". it can.
fun main() {
println("Hello Kotlin!")
}
When I installed IntelliJ, I wondered if that was the case, but the Kotlin file extension is ".kt". (Beginner)
There is a Run button to the left of the code editing the .kt file that you can click to run it. IntelliJ very convenient.
The file name is KotlinClassSono1.kt, but I was wondering if the Kotlin file is a class file.
How do you call this main function from Java? That's why there is a Jar file ... Even if I execute "Build Project" in IntelliJ, nothing is output, so I checked it.
Open Project Structure from the File menu. Click the "+" button in Project Settings> Artifacts JAR> From modules with dependencies Click the "OK" button without specifying the Main Class Check "Include in project build". When I run "Build Project" again ... The Jar file is output under the project folder. Thank you IntelliJ By the way, the project name is "Kotlin Component". If you look inside the Jar file It was confirmed that "KotilnClassSono1Kt.class" named "File name + Kt" was included.
When I read the Jar file created in the Java project,
public class Main {
public static void main(String[] args) {
KotlinClassSono1Kt.main();
}
}
You can see KotlinClassSono1Kt, I was able to execute Kotlin code from Java. Kt is attached to the class name, so I wondered what it is, so I went back to IntelliJ and added a package and class. Create a package with the context menu New> Package in the src folder of the package. it is normal. In the created Package, the context menu New> Kotlin File / Class !? For the first time, I noticed the difference between File and Class.
package ari
class KotlinClassSono2 {
}
"Ari" is specified for Package. Let's imitate Kotlin's Class documentation and give the class the function "test". The constructor is a bonus.
package ari
class KotlinClassSono2 constructor(name: String){
init {
println("name = ${name}")
}
constructor(name: String, age: Int) : this("$name"){
println("age = ${age}")
}
fun test() {
println("test call OK !")
}
}
Run "Build Project" again to create a Jar file and load it in Java. I saw the "Kotlin Class Sono2" class under the "ari" package. The class name does not have "Kt". I see ~ I also created an instance using the constructor. The function call is also successful.
Kotlin is good. I don't use it, but I thought I should read the reference a little more.
Recommended Posts