Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Gigasecond in Kotlin

// Calculate the moment when someone has lived for 10^9 seconds.
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month

fun main(args: Array<String>) {
    // Should print 2043-01-01T01:46:40
    println(Gigasecond(LocalDate.of(2011, Month.APRIL, 25)).date)
    
    // Should print 2009-02-19T01:46:40
    println(Gigasecond(LocalDate.of(1977, Month.JUNE, 13)).date)
}

data class Gigasecond(val dobWithTime: LocalDateTime) {
    constructor(dateOfBirth: LocalDate) : this(dateOfBirth.atStartOfDay())

    private val GIGASECOND = Math.pow(10.0, 9.0).toLong()
    val date: LocalDateTime = dobWithTime.plusSeconds(GIGASECOND)
}

FirstTry

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

코틀린예외처리

import java.io.BufferedReader
import java.io.FileReader

/**
 * Created by snake on 17. 5. 23.
 */
fun main(args : Array<String>){
    // java와 흡사하다. 그러나 checked excpetion을 지원안함.
    // 즉, try catch를 강제적으로 할 필요가 없다는 말임.
    // 그것보다는 알아서 방어코드를 만들라는 것이 kotlin 철학임.
    try{
        13 / 0;
    } catch(e: Exception){
        println(e);
    } finally {
        println("마지막 수행.")
    }


    var zerodivided = 13 / 0;
    println(zerodivided)
}

// try catch를 강제하지 않았을 뿐, 방어코드는 필요하다.
fun no_checked_exception(){
    // java 코드 자동컨버팅

//    try {
//        val `in` = BufferedReader(FileReader(args[0]))
//        var s: String?
//
//        s = `in`.readLine()
//        while (s != null) {
//            println(s)
//            s = `in`.readLine()
//        }
//        `in`.close()
//    } catch (e: IOException) {
//        System.err.println(e) // 에러가 있다면 메시지 출력
//        System.exit(1)
//    }
//

    // kotlin은 try .. catch문을 반드시 할필요가 없다. checked exception을 지원안함!!
    // ㅜㅜ
    val `in` = BufferedReader(FileReader("file경로명"))
    var s: String?

    s = `in`.readLine()
    while (s != null) {
        println(s)
        s = `in`.readLine()
    }
    `in`.close()

}

Compile and Execute Kotlin Online

fun main(args: Array<String>) {
    val a = 12
    val b = 25
    println(b + a)
    println(b - a)
    println(b / a)
    println( a * b)
    println(b % a )
    println( 25.0 / 12)
    
}

Compile and Execute Kotlin Online

fun main(args: Array<String>)  {
    val str = "May the Force be with you."
    
    println(str)
    
    val rawCrawl = """|long time ago,
    |in a galaxy
    |far far , away
    |BUMM, BUUM , BUUMM"""".trimMargin()
    println(rawCrawl)
    
   // for (char in str) {
     //   println(char)
 //   }
    val contentEquals = str.contentEquals("May the Force be with you.")
    println(contentEquals)
    
    val contains = str.contains("Force", true)
    println(contains)
    
    val uppercase = str.toUpperCase()
    val lower = str.toLowerCase()
    println(uppercase)
    println(lower)
    
    var num = 20
    
    var stringNum = num.toString()
    println(stringNum)
    val subsequence = str.subSequence(4,13)
    println(subsequence)
    
    val name = "Luke Skywalker"
    val color = "green"
    
    val car = "Landspeeder"
    val age = 27

    
    println("$name has a $color lightsaber and drive a $car and is $age")
    println("luke full name $name has ${name.length}")


}

test

fun main(args: Array<String>) { 
    var rawString :String ="I am Raw String!" 
    val escapedString : String ="I am escaped String!\n" 
    println("Hello!"+escapedString) 
    println("Hey!!"+rawString) 
     
}

Characters Output

fun main(args: Array<String>)
{
    val letter: Char
    letter = 'a'
    println("$letter")
}

https://pt.stackoverflow.com/q/256697/101

fun main(args: Array<String>) {
    println("Texto Em Caixa".equals("texto em caixa", ignoreCase = true));
    println("Texto Em Caixa".equals("texto em caixa", ignoreCase = false));
}

//https://pt.stackoverflow.com/q/256697/101

17.functionextension

fun main(args : Array<String>){
    var 문자열 : String  = "일반 문자열";
    println (
        문자열.추가함수("추가함수")
    );

}

// 이미 만들어진 객체에 함수를 추가할 수 있다.
fun String?.추가함수(s : String ) : String{
    return "나는 $this 너는 $s";
}

14.interface_abstract_static

fun main(args : Array<String>){

    // 인터페이스
    인터페이스구현().반드시구현해야하는함수();
    var obj : 인터페이스;

    // 추상화
    // 아래 코드는 다음과 같이도 가능함
    // 추상화클래스구현().apply { 그냥함수();  상속받으면구현하세요(); }
    var 객체 = 추상화클래스구현();
    객체.그냥함수();
    객체.상속받으면구현하세요();

    // static
    println( 추상화클래스구현.스태틱변수);
    추상화클래스구현.이거스태틱임() ;
}

interface 인터페이스{
    fun 반드시구현해야하는함수();
}

class 인터페이스구현 : 인터페이스{
    override fun 반드시구현해야하는함수() = println ("구현했음");
}

abstract class 추상화클래스{
    fun 그냥함수() = println("그냥함수");
    abstract fun 상속받으면구현하세요();
}

class 추상화클래스구현 : 추상화클래스(){
    override fun 상속받으면구현하세요() = println("상속구현했음");

    // companion object Factory {} 안에서 구현해야 static 가능
    companion object Factory {
        var 스태틱변수 = "스태틱변수";
        fun 이거스태틱임()= println("이거스태틱 함수임");
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.