Kotlin is a modern, statically typed, general-purpose programming language developed by JetBrains. It is designed to be fully interoperable with Java and primarily targets the Java Virtual Machine (JVM), but also compiles to JavaScript (Kotlin/JS) for web applications and native code (Kotlin/Native) via LLVM for platforms like iOS, macOS, Linux, and Windows.
basic data types
In total, Kotlin has the following basic types:
Category
Basic types
Example code
Integers
Byte, Short, Int, Long
val year: Int = 2020
Unsigned integers
UByte, UShort, UInt, ULong
val score: UInt = 100u
Floating-point numbers
Float, Double
val currentTemp: Float = 24.5f, val price: Double = 19.99
Booleans
Boolean
val isEnabled: Boolean = true
Characters
Char
val separator: Char = ','
Strings
String
val message: String = "Hello, world!"
Unsigned integers(无符号整数): An unsigned integer is a whole number variable in computer science that can only represent non-negative values (zero and positive numbers). Unlike signed integers, which can represent both positive and negative numbers, unsigned integers allocate all available bits to represent the magnitude of the number, allowing for a larger range of positive values for a given number of bits.
For more information on basic types and their properties, see Basic types.
With this knowledge, you can declare variables and initialize them later. Kotlin can manage this as long as variables are initialized before the first read.
To declare a variable without initializing it, specify its type with :. For example:
// Variable declared without initializationval d: Int// Variable initializedd = 3// Variable explicitly typed and initializedval e: String = "hello"// Variables can be read because they have been initializedprintln(d) // 3println(e) // hello
If you don’t initialize a variable before it is read, you see an error:
// Variable declared without initializationval d: Int// Triggers an errorprintln(d)// Variable 'd' must be initialized
Explicitly declare the correct type for each variable:
fun main() { val a: Int = 1000 val b: String = "log message" val c: Double = 3.14 val d: Long = 100_000_000_000_000 val e: Boolean = false val f: Char = '\n'}
can be simplified
fun main() { val a: Int = 1000 val b = "log message" val c = 3.14 val d = 100_000_000_000_000 val e = false val f = '\n'}
If you use the Array class with primitive values, these values are boxed into objects. As an alternative, you can use primitive-type arrays, which allow you to store primitives in an array without the side effect of boxing overhead:
These classes have no inheritance relation to the Array class, but they have the same set of functions and properties.
This example creates an instance of the IntArray class:
fun main() {//sampleStart // Creates an array of Int of size 5 with the values initialized to zero val exampleArray = IntArray(5) println(exampleArray.joinToString()) // 0, 0, 0, 0, 0//sampleEnd}
I/O
password.txt放大 resource目录
fun main() { println(readFile("password.txt")) // readFile} fun readFile(fileName: String): String { // 通过当前类的class获取stream // 不使用类加载器`ClassLoader` val stream = object {}.javaClass.getResourceAsStream("/$fileName") ?: throw IllegalArgumentException("Resource not found: $fileName") return stream.bufferedReader().use { it.readText() } }
companion object{ val PATH: Path = FabricLoader.getInstance().gameDir.resolve("genshin_impact_config")}private val configFile: File get() = PATH.resolve("config.json").toFile()fun saveConfig(){ if(!PATH.toFile().exists()){ PATH.createDirectories() } try { val modules = Genshin.moduleManager.getModules() val map = HashMap<String, Boolean>() modules.forEach { module -> // 以模块名为 key,保存是否启用 map[module.javaClass.simpleName] = module.isEnabled() // 遍历每个module,加入 map集合 } FileWriter(configFile).use { writer -> gson.toJson(map, writer) } Genshin.LOGGER.info("Saved config to ${configFile.absolutePath}") } catch (e: IOException) { Genshin.LOGGER.error("Failed to save config", e) }}