forked from JetBrains/kotlin-web-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc(docs.topics.jvm.javaToKotlinInterop): add notes and examples
- Loading branch information
alfredo-toledano
committed
Aug 27, 2024
1 parent
8f22fd3
commit 86c5179
Showing
4 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
docs/topics/jvm/java-to-kotlin-interop/JavaToKotlinInterOp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters