简体   繁体   中英

How to define Long Long data type in kotlin?

fun main(args: Array<String>) {
    try {
        var sum: Long = 0
        val n: Int = readLine()!!.toInt()

        for (i in 0..(n - 1)) {
            var input: Long?
            input = readLine()!!.toLong()
            sum += input
        }
        println(sum)
    } catch (ex: Exception) {
        println(ex.message)
    } 
}

I want to take data Type Long Long replacing at Long . So how can I define Long Long data type?

Kotlin's Long is 64-bit already. No need for ancient long long trickery:

https://kotlinlang.org/docs/reference/basic-types.html

If you're on the JVM, there isn't a long long type, but you could use java.math.BigInteger for arbitrarily large numbers instead.

See more discussion on this topic and some more alternatives at a Java question here .

Kotlin handles long long data type with the BigInteger data type. Replace the long with BigInteger;

fun main(args: Array<String>) {
    try {
        var sum = 0.toBigInteger()
        val n: Int = readLine()!!.toInt()

        for (i in 0..(n - 1)) {
            var input: BigInteger?
            input = readLine()!!.toBigInteger()
            sum += input
        }
        println(sum)
    } catch (ex: Exception) {
        println(ex.message)
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM