Skip to content

Commit

Permalink
doc(docs.topics.jvm.javaToKotlinInterop): add notes and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredo-toledano committed Aug 27, 2024
1 parent 8f22fd3 commit 86c5179
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
16 changes: 16 additions & 0 deletions docs/topics/jvm/java-to-kotlin-interop/JavaToKotlinInterOp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class JavaToKotlinInterOp {
public static void main(String[] args) {
// Creating an instance of the Kotlin class
Person person = new Person("Alice", 30);

// Accessing the properties and methods
String greeting = person.greet();

// Printing the result
System.out.println(greeting);

// Directly accessing the fields (Java syntax)
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
5 changes: 5 additions & 0 deletions docs/topics/jvm/java-to-kotlin-interop/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data class Person(val name: String, val age: Int) {
fun greet(): String {
return "Hello, my name is $name and I am $age years old."
}
}
15 changes: 15 additions & 0 deletions docs/topics/jvm/java-to-kotlin-interop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Goal
* instances of a Kotlin class -- can be seamlessly -- created and operated | ".java"

## Prerequisites
* [Install the compiler locally](https://kotlinlang.org/docs/command-line.html#install-the-compiler)
* Define an environment variable to the kotlin root path installation(`HOMEBREW_KOTLIN_ROOT`)

## How to create & run an application?
* The application will be just 1! `.kt` file
* `kotlinc Person.kt -d .`
* create the `Person.class`
* `javac -cp .:$HOMEBREW_KOTLIN_ROOT/lib/kotlin-stdlib.jar JavaToKotlinInterOp.java`
* create the `JavaToKotlinInterOp.class`
* `java -cp .:$HOMEBREW_KOTLIN_ROOT/lib/kotlin-stdlib.jar JavaToKotlinInterOp`
* run the application
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[//]: # (title: Calling Kotlin from Java)

Kotlin code can be easily called from Java.
For example, instances of a Kotlin class can be seamlessly created and operated in Java methods.
However, there are certain differences between Java and Kotlin that require attention when
integrating Kotlin code into Java.
On this page, we'll describe the ways to tailor the interop of your Kotlin code with its Java clients.
* from Java -- can easily call -> Kotlin code
* _Example:_ instances of a Kotlin class -- can be seamlessly -- created and operated | ".java"
* if you integrate Kotlin code | Java -> check differences Java vs Kotlin
* ⭐goal of this document ⭐

## Properties

Expand Down Expand Up @@ -135,7 +134,7 @@ class JavaClient {
The visibility of the field will be the same as the visibility of `lateinit` property setter.

## Static fields

* TODO
Kotlin properties declared in a named object or a companion object will have static backing fields
either in that named object or in the class containing the companion object.

Expand Down

0 comments on commit 86c5179

Please sign in to comment.