java代码可以调用kotlin方法吗,从Java调用Kotlin函数是Java中的关键字?

本文探讨了Java中如何调用Kotlin的非关键字函数,通过创建别名或避免使用Java保留字作为函数名,提供了解决方案和编译器设置建议。展示了如何在Kotlin中定义和在Java中调用带有'new'关键字的函数,并给出了实际的代码示例和反编译后的Java字节码分析。
摘要由CSDN通过智能技术生成

Since new is not a keyword in kotlin, i can have the following function in kotlin.

fun new(): String {

return "just returns some string"

}

But i am unable to call this function from java since new is a keyword in java. I would like to know if there is some alias for this function in java realm. I did not find any intellij suggestions that might be a possible alias to this function.

Edit 1:

I have written the following code in kotlin:

fun new(): String {

return "just returns some string"

}

fun main(args:Array){

new()

}

And I had a look at the java bytecode. It was as follows.

// ================MainKt.class =================

// class version 50.0 (50)

// access flags 0x31

public final class MainKt {

// access flags 0x19

public final static new()Ljava/lang/String;

@Lorg/jetbrains/annotations/NotNull;() // invisible

L0

LINENUMBER 2 L0

LDC "just returns some string"

ARETURN

L1

MAXSTACK = 1

MAXLOCALS = 0

// access flags 0x19

public final static main([Ljava/lang/String;)V

@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0

L0

ALOAD 0

LDC "args"

INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V

L1

LINENUMBER 6 L1

INVOKESTATIC MainKt.new ()Ljava/lang/String;

POP

L2

LINENUMBER 7 L2

RETURN

L3

LOCALVARIABLE args [Ljava/lang/String; L0 L3 0

MAXSTACK = 2

MAXLOCALS = 1

@Lkotlin/Metadata;(mv={1, 1, 6}, bv={1, 0, 1}, k=2, d1={"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\u0008\u0003\u001a\u0019\u0010\u0000\u001a\u00020\u00012\u000c\u0010\u0002\u001a\u0008\u0012\u0004\u0012\u00020\u00040\u0003\u00a2\u0006\u0002\u0010\u0005\u001a\u0006\u0010\u0006\u001a\u00020\u0004\u00a8\u0006\u0007"}, d2={"main", "", "args", "", "", "([Ljava/lang/String;)V", "new", "production sources for module Srinivas"})

// compiled from: Main.kt

}

// ================META-INF/production sources for module Srinivas.kotlin_module =================

MainKt

And here is the bytecode decompiled to java:

import kotlin.Metadata;

import kotlin.jvm.internal.Intrinsics;

import org.jetbrains.annotations.NotNull;

@Metadata(

mv = {1, 1, 6},

bv = {1, 0, 1},

k = 2,

d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0003\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005\u001a\u0006\u0010\u0006\u001a\u00020\u0004¨\u0006\u0007"},

d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "new", "production sources for module Srinivas"}

)

public final class MainKt {

@NotNull

public static final String new() {

return "just returns some string";

}

public static final void main(@NotNull String[] args) {

Intrinsics.checkParameterIsNotNull(args, "args");

new();

}

}

It appears that writing a function named new is a valid java bytecode. But javac is not letting me compile the code. Is there some annotation or compiler flag I can enable to get javac to compile the java file with call to this function.

解决方案

Since you're trying to use a keyword the compiler won't allow you to use it as a method name

The only workaround I can think is to do something like:

fun new(): String {

return "just returns some string"

}

fun notAKeyWord() = new()

So you can use WhatEverKt.notAKeyWord() from your java code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值