Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Could you please tell me how to run java code in kotlin file in this compiler.

+4 votes
asked Mar 16, 2023 by talha noob (160 points)

Kotlin Code
fun main() {     var myjavaobject = Numbers()     myjavaobject.myfunction() } 


Java Code
public class Numbers {     public void myfunction(){         System.out.println("I am from java file. My class name is Numbers. I'm getting called by kotlin code\n");     } } 
I want to know how can I use java code in my above kotlin code.

1 Answer

0 votes
answered Mar 26, 2023 by Gédéon KOUMAKO (180 points)

To use the Numbers class defined in the Java code from your Kotlin code, you can create an instance of the Numbers class in your Kotlin code using the new keyword, and then call its myfunction method. Here's an example:

Java code (Numbers.java):

public class Numbers { public void myfunction(){ System.out.println("I am from java file. My class name is Numbers. I'm getting called by kotlin code\n"); } }

Kotlin code (Main.kt):

fun main() { val myJavaObject = Numbers() myJavaObject.myfunction() }

In this code, we create an instance of the Numbers class using the new keyword and store it in the myJavaObject variable. We can then call the myfunction method on this object to print out the message defined in the Java code.

Make sure that the Java code is in the same directory as your Kotlin code and that the Java code is compiled before you run your Kotlin code.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...