Tag: jvm

Kotlin应用程序与Gradle应用程序插件

我正在尝试使用kotlin , gradle和gradle application插件来创建一个简单的HelloWorld应用application 。 当我用下面的设置运行它时,出现以下错误: Error: Main method is not static in class com.petarkolaric.helloworld.Main, please define the main method as: public static void main(String[] args) 我的build.gradle : group ‘helloworld’ version ‘1.0-SNAPSHOT’ buildscript { ext.kotlin_version = ‘1.2.0’ repositories { mavenCentral() } dependencies { classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } apply plugin: ‘kotlin’ apply plugin: ‘application’ mainClassName = […]

使用Kotlin的数字操作是否像Java原语一样快?

Java有原始语言,因为使用它们会导致比基于类的对应语言更高效,可读和更少错误的代码 。 Kotlin是否执行编译时优化以确保编号操作与Java基元相同(或更好)的性能?

错误:无法find或加载主类Hello.class

尝试最简单的kotlin你好世界可能: thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ ll total 32 drwxr-xr-x 2 thufir thufir 4096 Oct 27 07:28 ./ drwx—— 46 thufir thufir 16384 Oct 27 06:47 ../ -rw-r–r– 1 thufir thufir 104 Oct 27 07:27 Hello.kt thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ kotlinc Hello.kt WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) WARNING: […]

将@JvmStatic和@JvmField自动应用于Kotlin中的所有文件

我想知道这是否可以通过编译器参数或插件完成。 我有现成的java模块,我转换为kotlin,但它有一堆静态方法/字段。 在某些时候,我想重构一下,但现在我的(Java)这个库的消费者不工作,除非我手动添加缺少@JvmStatic/@JvmField注释伴侣对象字段和方法。 有没有办法自动做到这一点? 谢谢

Kotlin有垃圾收集器吗? 如果是这样,基于哪种算法?

我正在Kotlin上做一个学校项目,需要知道它如何处理垃圾。 它在垃圾回收器中与Java相似吗?

操作数堆栈上的错误types与Kotlin协同程序

这段代码与Kotlin 1.2.10编译成功,但是当我运行它时,它会java.lang.VerifyError: Bad type on operand stack产生一个java.lang.VerifyError: Bad type on operand stack 。 如果我删除了Test类并将其提取出来,它将按预期工作。 这是为什么? import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.runBlocking import java.util.* import kotlin.concurrent.schedule class Test { fun scheduleTimeout() { Timer(true).schedule(300) { launch { runSuspended(“hello”) } } } suspend fun runSuspended(txt: String) = println(txt) } fun main(args: Array) { Test().scheduleTimeout() runBlocking { delay(10000) } […]

使用Kotlin协程的multithreading

我正在用Kotlin Coroutines进行试验,并有如下代码: fun main(args: Array) = runBlocking { val cores = Runtime.getRuntime().availableProcessors() println(“number of cores: $cores”) val jobs = List(10) { async(CommonPool) { delay(100) println(“async #$it on thread ${Thread.currentThread().name}”) } } jobs.forEach { it.join() } } 这是我的输出: number of cores: 4 async number:0 on thread ForkJoinPool.commonPool-worker-2 async number:2 on thread ForkJoinPool.commonPool-worker-3 async number:3 on thread […]

将Kotlin .class文件打包到JAR中执行

在关于“Kotlin – 编译并从Windows命令行运行”的教程之后,有一个缺失的清单: thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ ll total 32 drwxr-xr-x 2 thufir thufir 4096 Oct 27 08:29 ./ drwx—— 46 thufir thufir 16384 Oct 27 08:03 ../ -rw-r–r– 1 thufir thufir 107 Oct 27 08:29 HelloWorld.kt thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by […]

kotlin – 通过函数的方法引用

比方说,我有以下的Java类: public class A { public Result method1(Object o) {…} public Result method2(Object o) {…} … public Result methodN(Object o) {…} } 然后,在我的Kotlin代码中: fun myFunction(…) { val a: A = … val parameter = … val result = a.method1(parameter) // what if i want methodX? do more things with result } 我希望能够选择在myFunction内部调用哪个methodX。 在Java中,我会传递A::method7作为参数并调用它。 在Kotlin它不编译。 我应该如何解决在Kotlin?

为JVM实现C#

有没有人试图为JVM实现C#? 作为一名Java开发人员,我一直嫉妒C#,但不愿意放弃JVM的可移植性和成熟性,更不用说为它提供各种各样的工具了。 我知道JVM和CLR之间有一些重要的区别,但是有没有什么是最好的?