Skip to content

update: java interop page - #5875

Open
Danil Pavlov (danil-pavlov) wants to merge 5 commits into
masterfrom
java-interop-upd
Open

Danil Pavlov (danil-pavlov) wants to merge 5 commits into
masterfrom
java-interop-upd

Conversation

@danil-pavlov

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Comment thread docs/topics/jvm/java-interop.md Resolved
Co-authored-by: Alexander Udalov <udalov@users.noreply.github.com>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice updates! I added a few comments and suggestions

and Kotlin code can be used from Java as well. This page describes some details about calling Java code from Kotlin.

Pretty much all Java code can be used without any issues:
Most Java code can be used without additional configuration:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could remove passive from here:
You can use most Java code without additional configuration:

val calendar = Calendar.getInstance()
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // call getFirstDayOfWeek()
calendar.firstDayOfWeek = Calendar.MONDAY // call setFirstDayOfWeek()
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // Call getFirstDayOfWeek()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // Call getFirstDayOfWeek()
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // Calls getFirstDayOfWeek()

if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // call getFirstDayOfWeek()
calendar.firstDayOfWeek = Calendar.MONDAY // call setFirstDayOfWeek()
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // Call getFirstDayOfWeek()
calendar.firstDayOfWeek = Calendar.MONDAY // Call setFirstDayOfWeek()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
calendar.firstDayOfWeek = Calendar.MONDAY // Call setFirstDayOfWeek()
calendar.firstDayOfWeek = Calendar.MONDAY // Calls setFirstDayOfWeek()

}
if (!calendar.isLenient) { // call isLenient()
calendar.isLenient = true // call setLenient()
if (!calendar.isLenient) { // Call isLenient()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!calendar.isLenient) { // Call isLenient()
if (!calendar.isLenient) { // Calls isLenient()

if (!calendar.isLenient) { // call isLenient()
calendar.isLenient = true // call setLenient()
if (!calendar.isLenient) { // Call isLenient()
calendar.isLenient = true // Call setLenient()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
calendar.isLenient = true // Call setLenient()
calendar.isLenient = true // Calls setLenient()

fun render(list: List<*>, to: Appendable) {
for (item in list) {
to.append(item.toString()) // Java would require us to catch IOException here
to.append(item.toString()) // Java would require catching IOException here because Appendable.append throws IOException

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to.append(item.toString()) // Java would require catching IOException here because Appendable.append throws IOException
to.append(item.toString()) // In Java, render() must catch or declare the IOException thrown by Appendable.append()

Java reflection works on Kotlin classes and vice versa. As mentioned above, you can use `instance::class.java`,
`ClassName::class.java` or `instance.javaClass` to enter Java reflection through `java.lang.Class`.
Do not use `ClassName.javaClass` for this purpose because it refers to `ClassName`'s companion object class,
Do not use `ClassName.javaClass` for this purpose because it refers to `ClassName`'s [companion object](object-declarations.md#companion-objects) class,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Do not use `ClassName.javaClass` for this purpose because it refers to `ClassName`'s [companion object](object-declarations.md#companion-objects) class,
Don't use `ClassName.javaClass` for this purpose because it refers to `ClassName`'s [companion object](object-declarations.md#companion-objects) class,


According to Java's rules, `finalize()` must not be `private`.

> `finalize()` is deprecated in modern Java versions. Consider alternatives such as [`java.lang.ref.Cleaner`](https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Cleaner.html)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> `finalize()` is deprecated in modern Java versions. Consider alternatives such as [`java.lang.ref.Cleaner`](https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Cleaner.html)
> The `finalize()` method is deprecated in modern Java versions. Consider alternatives such as [`java.lang.ref.Cleaner`](https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Cleaner.html)

Other supported cases include acquiring a Java getter/setter method or a backing field for a Kotlin property, a `KProperty` for a Java field, a Java method or constructor for a `KFunction` and vice versa.

Other supported cases include acquiring a Java getter/setter method or a backing field for a Kotlin property,
a `KProperty` for a Java field, a Java method or constructor for a `KFunction` and vice versa.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
a `KProperty` for a Java field, a Java method or constructor for a `KFunction` and vice versa.
a `KProperty` for a Java field, a Java method or constructor for a `KFunction`, and a `KFunction` for a Java method or constructor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove latin


> SAM conversions only work for interfaces, not for abstract classes, even if those also have just a single
abstract method.
> SAM conversions only work for interfaces, not for abstract classes, even if those also have just a single abstract method.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> SAM conversions only work for interfaces, not for abstract classes, even if those also have just a single abstract method.
> SAM conversions only work for interfaces. They don’t apply to abstract classes, even when those classes have only one abstract method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants