Skip to content

Instantly share code, notes, and snippets.

@dav1dnix
Created April 1, 2020 16:28
Show Gist options
  • Save dav1dnix/a25cde549f345be5b4b6ac9fb62e62fb to your computer and use it in GitHub Desktop.
Save dav1dnix/a25cde549f345be5b4b6ac9fb62e62fb to your computer and use it in GitHub Desktop.
kotlin 8)
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
fun a(b: Int) {
val a = b
print("Number $a in binary is ${Integer.toBinaryString(a)}")
}
// Can only be called on "Double" because no implicit widening conversions
fun double(d: Double) {
print(d)
}
fun printDouble(d: Double) { print(d) }
val e = 3.2e10
fun abcd() {
/* Can't assign value of smaller type Byte to Int, therefore getting an error
* smaller types are not implicitly converted to bigger types
*/
val asd: Byte = 1
// val dsfs: Int = asd (Error)
// However, you can use explicit conversions to widen numbers
val dsfs: Int = asd.toInt() // Now it will work fine :D
println("${asd}\n${dsfs}") // returns 1
//1
}
/* Difference between Int and Int? is that Int? is a "nullable number reference"
* meaning that it can be null
* whereas Int without "?" cannot be null
* Int is a primitive type
* Here is an example of what I just said
*/
fun nullNotNull() {
//val num1: Int = null // "Null can not be a value of a non-null type Int"
val num2: Int? = null // This gives no error
//println("${num1}\n${num2}")
}
fun array() {
// Use array constructor
// Takes in array size and returns initial value of each array element given its index
val array = Array(5) { a -> (a*a).toString() }
array.forEach { println(it) }
/*
* Returns 0 \n 1 \n 4 \n 9 \n 16
*/
// Array elements are returned as strings
val elementIsString = array.get(2)
// print(elementIsString) // returns the 3rd element
print("${elementIsString::class.simpleName}") // Returns "String"
}
// array()
// nullNotNull()
// abcd()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment