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<String>){ 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.
One extremely useful feature in IntelliJ IDEA and Android Studio is the ability to see the compiled bytecode of your Kotlin code and then the decompiled Java code. For this, press Ctrl+Shift+A (Cmd+Shift+A on Mac) to invoke the action search, then type “Show Kotlin Bytecode” (or “skb”) and press Enter.
If we want to call the Kotlin code from Java class both present inside the different packages, this requires to import the package name with Kotlin file name inside Java class and calling the Kotlin code from Java class. Another way is to give full path as packageName.
If your question is can you use kotlin files in java files and vice versa then the answer is yes.
To define a function in Kotlin, fun keyword is used. Then comes the name of the function (identifier). Here, the name of the function is callMe .
For these sorts of problems, you can use the @JvmName
annotation.
@JvmName("neww") fun new(): String { return "just returns some string" }
The name you pass to it will be the name that you can use to refer to the method from Java:
String s = something.neww();
In general, you're probably better off not using Java keywords as Kotlin identifiers if you need to interop with Java code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With