Kotlin is an object-oriented programming language developed by JetBrains, famous for IntelliJ IDEA. Because the compiled code runs on the JVM, It is now possible to divert the assets created in JAVA so far.
//Declaring with val prevents reassignment of values
val firstName: String = "Tanaka"
//No need to define each type in type inference
val lastName = "Taro"
//Can be reassigned by declaring it with var
var age = 20
age = 21
//Array(array)Create
val nameList: Array<String> = arrayOf("tanaka", "saitou", "kimura")
println(nameList[0]) //output by tanaka
//Rewriting elements of an array
nameList[1] = "sakurai"
println(nameList[1]) //output by sakurai
//Array(list)Create
val animalList: List<String> = listOf("dog", "cat", "rabbit")
println(animalList[0]) //dog outputs
//Rewriting elements of an array
animalList[1] = "tiger" //Error interface List is read-only
Even with List, mutableListOf () can be used to create a list that can be changed, so I'm not sure how to use Array and List properly.
//Map creation
val numberMap: MutableMap<String, Int> = mutableMapOf("one" to 1)
println(numberMap["one"]) //1 is output
////Add value
numberMap["tow"] = 2
println(numberMap) // {one=1, tow=2}Outputs
//Map with mapOf
val reNumberMap: Map<Int, String> = mapOf(1 to "one")
println(reNumberMap[1]) //1 is output
////Add value
reNumberMap["tow"] = 2 //Error mapOf is read-only
//Ordinary if statement
if (true) {
println("if") //if outputs
} else {
println("else")
}
//Kotlin doesn't have a ternary operator, so it looks like that
val animal = "dog"
val isDog = if (animal == "dog") true else false
println(isDog) //true is output
//For empty check
val person:String? = null
val personName = person?: "NoName"
println(personName) //NoName is output
//When statement Similar to switch statement
val result = when("hoge") {
"hoge" -> "hoge"
"fuga" -> "fuga"
else -> "else"
}
println(result) //hoge outputs
//while statement
var count = 5
while(0 < count--) {
println("while count: ${count}")
}
//for statement
for (i in 1..5) {
println("for count: ${i}")
}
Add age, name, and gender to the user class I wrote the code to execute the self-introduction method.
Kotlin
user.kt
fun main() {
val user = userData(20, "Taro", "Men")
user.selfIntroduction()
}
userData.kt
data class userData (
var age: Int? = 0,
var name: String = "NoName",
var gender: String? = null
) {
fun selfIntroduction() {
println("My name is ${name}")
println("Age is ${age}")
println("Gender is ${gender}")
}
}
JAVA
user.java
public class user {
public static void main(String[] args) {
userData user = new userData(20, "Taro", "Men");
user.selfIntroduction();
}
}
userData.java
public class userData {
private int age;
private String name;
private String gender;
public userData(int age, String name, String gender) {
this.age = age;
this.name = name;
this.gender = gender;
}
public void selfIntroduction() {
System.out.println("My name is " + this.name);
System.out.println("Age is " + this.age);
System.out.println("Gender is " + this.gender);
}
}
The difference I felt after writing
Windows10 Pro IntelliJ Community Edition 2020.2 JAVA 14
Use Spring Initializr to get the template of the project.
The setting is Project : Gradle Project Language : Kotlin Spring Boot : 2.3.4 ** Project Metadata **: Leave default Packaging Jar Java : 14 Dependencies : Spring Web
Finally, press ** GENERATE ** to download the project template
From IntelliJ ** File → Open ... → Select the template of the downloaded project → Extract with the procedure of OK **
For the demo / src / main / kotlin / com.example.demo / controller
folder
** Right click → New → Press Kotlin File / Class **
Create HelloController.kt.
HelloController.kt
package com.example.demo.controller
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
@RestController
public class HelloController {
@RequestMapping("/")
fun hello():String {
return "Hello World"
}
}
Click the ** play button ** on the upper right to build
Connect to http: // localhost: 8080
when the build is finished
I haven't touched JAVA so much, so I was worried that Kotlin would be okay without knowing JAVA. When I suddenly started writing from Kotlin, there was no problem with Kotlin suddenly Rather, I found it easier to get started with Kotlin. When I compared it with JAVA, the amount of code in Kotlin became smaller, I felt that type inference, data classes, etc. helped keep the code simple.
Recommended Posts