随笔分类 - Kotlin
摘要:For example we have tow Entities: package com.virtualpairprogrammers.theater.domain import javax.persistence.* @Entity data class Performance( @Id @Ge
阅读全文
摘要:pom.xml: add compile plugin <build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <testSourceDirectory>${project.basedir}/src/
阅读全文
摘要:pom.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <gr
阅读全文
摘要:Structure: tempalates/seatBooking.html: <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>com.
阅读全文
摘要:In Java, you can use Autowired to init the service: @Autowired TheaterService theaterService In kotlin, it is different, you need to use 'lateinit var
阅读全文
摘要:Backing bean: A typical JavaServer Faces application includes one or more backing beans, each of which is a type of JavaServer Faces managed bean that
阅读全文
摘要:Install devtool: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <option
阅读全文
摘要:In Kotlin, you are able to extend a class. Just like add prototype in Javascript. fun toSentenceCase(a: String): String { return a[0].toUpperCase() +
阅读全文
摘要:import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import java.util.* class AgeCalculation() { fun getAge(dob: Calendar): Int {
阅读全文
摘要:In Java, when you write code in try(...), if exception throw, the resource will be closed automactially: This is not necessary in Kotlin fun printFile
阅读全文
摘要:In java, it requires you to handle the exception when you declaring and using the code: public class BoringJavaCode { public static Double divide(int
阅读全文
摘要:List is not mutable, when you want to add data into list, you can do is: "adding data to a mutableList", then return a immutable list by calling toLis
阅读全文
摘要:(0..9).forEach(::println) (9 downTo 0).forEach(::println) (0 until 9).forEach(::println) // 0 to 8 (0..9 step 2).forEach(::println) ('A'..'F').forEach
阅读全文
摘要:fun getColorType() : String { val color = getUpperFavoriteColor() return when (color) { "" -> "empty" "RED", "GREEN", "BLUE" -> "rgb" else -> "other"
阅读全文
摘要:Let function is another way to solve null problem in kotlin. When you have such problem: var favoriteColor: String? = null...... return if f(avoriteCo
阅读全文
摘要:In Javascript, we have: return age null ? -1 : age In kotlin, we can do the same thing with a better / simpler syntax: val safeAge: Int get() = age ?:
阅读全文
摘要:For data class, you can get 'copy' method, and also destructing: data class Customer (val name: String, val address: String, var age: Int) { // if we
阅读全文
摘要:// Shorter version class AnotherAlternativeCustomer (val name: String,var age: Int, val address: String = "") { var approvered: Boolean = false set(va
阅读全文
摘要:package com.rsk class Customer (val name: String, val address: String, var age: Int) { // if we don't want to pass in the address constructor(name: St
阅读全文
摘要:val randomNumber = Random().nextInt(3) if (randomNumber is BigDecimal) { result = result.add(BigDecimal(36)) } If you use type check, then 'result' is
阅读全文