I read the Big Nerd Ranch guide to Kotlin and it talked in several places about Kotlin/Java interop, but never JS or native. I already had a solid background in Java, so I have gotten used to using Java classes in my Kotlin code. I am trying to write a Kotlin program which will be run on a site where most - if not all - functionality is written in JavaScript, and I am trying to understand how to write my code to make sure that it is interoperable. Will I be able to continue using Java classes in my Kotlin/JS code? What are the differences between writing Kotlin/JVM code and Kotlin/JS code? What should a (ex-) java programmer know when learning to interop with JS using Kotlin? If there are a few chapters on this in any good books written in the recent past, that would be helpful also.
Differences in writing Kotlin/JVM and Kotlin/JS?
2.1k Views Asked by Sam At
2
There are 2 best solutions below
0
CryptoFool
On
Kotlin/JS compiles Kotlin code, including its own standard library, into Javascript code. At the end, that's all you have is Javascript. What you don't have is any connection to the Java Virtual Machine. Kotlin's standard library provides no magic to bridge Javascript code to the JVM so that it can utilize Java classes. So NO, you can't utilize Java classes in standard Kotlin/JS.
Related Questions in KOTLIN
- Volatile properties in Kotlin?
- Using multiple JVM languages in the same project
- Kotlin - IntelliJ Project Setup
- Kotlin let analogue which returns the receiver
- Kotlin 'when' statement vs Java 'switch'
- Use Kotlin extension in android java class
- Kotlin stub/placeholder function for unimplemented code
- Partial class delegation in Kotlin
- SugarORM + Kotlin: Unresovled reference "listAll"
- Kotlin JS - string to number conversion?
- Where is the old Kotlin specification hosted?
- Kotlin JS - Accessing HTML DOM properties
- Access property delegate in Kotlin
- Kotlin-JS interop - using language constructs
- Does "internal" visibility modifier in Kotlin work yet?
Related Questions in KOTLIN-MULTIPLATFORM
- Kotlin Multiplatform - Generating JS library (nodejs module)
- Sample project using 'xcode-plugin' from org.openbakery.xcode-plugin
- Unable to add apple device in Android studio
- Workarounds to import java lib for mingw / ios / linus / other source sets?
- Kotlin mobile multiplatform library able to build in isolation as well as within android project
- How to apply -Xopt-in=kotlin.ExperimentalUnsignedTypes to all subprojects?
- Error: Could not find or load main class MainKt
- Listen to Kotlin coroutine flow from iOS
- Error Running Unit Tests for Kotlin Multiplatform Project
- no such table while inserting data in SQLDelight
- How to use default interface implementation with kotlin Multiplatform and Swift
- How to attach Kotlin sources of multiplatform dependencies to IDEA?
- Kotlin Multiplatform Library: Unable to generate .framework for iOS
- I create a new KMM project in Android studio and don't see the androidMain module in Android View
- Kotlin/Native: how to modify CArrayPointer?
Related Questions in KOTLIN-JS
- How to apply -Xopt-in=kotlin.ExperimentalUnsignedTypes to all subprojects?
- Execution failed for task ':rootPackageJson' while building the Kotlin JS project in Docker
- Include external ES modules in the bundle
- Persistent error: Unresolved reference: runBlocking when using kotlinx-coroutines-core-js:1.7.3
- KotlinJS React - Context and Reducer
- How can I force Dukat for generation of declared types?
- How to create an "options object" (Object with many optional properties) in Kotlin compiling to JavaScript?
- Getting command line arguments for Kotlin/JS Node process
- Setting output path for Kotlin/JS distribution / webpack output
- kotlinNpmInstall behind corporate proxy
- Why does my Kotlin app fail to load a Kotlin ServiceWorker script because 'kotlin' was not found?
- How to use/import TypeScript declaration files in JavaScript?
- KotlinJs : how to write HandleBarJS in kotlin
- I'm getting this error when trying to read a local JSON file in Kotlin/JS+React. What is going on?
- Kotlin/JS web app doesn't start on Heroku
Related Questions in KOTLIN-INTEROP
- Kotlin, project structure
- Can't have a return inside a Runnable SAM in Kotlin?
- Kotlin compiler's type inference can't choose which method to call (ambiguity with generic types)
- Assignment not allowed in while expression?
- how to use spring annotations like @Autowired or @Value in kotlin for primitive types?
- Kotlin syntax for inferring generic supertype from subtype
- Kotlin lambda / Java SAM interop - type mismatch
- Differences in writing Kotlin/JVM and Kotlin/JS?
- Converting Java To Kotlin: Type mismatch. Required: MenuSlidingTabStrip.OnTabSelectedListener? Found: (Nothing, Nothing) → Boolean
- Kotlin/Native GTK Interop
- Why do some Java setter methods automatically become Kotlin properties but some don't?
- How to use SQLite in Kotlin/Native?
- Data Binding: ObservableField with lambda value doesn't compile
- Required <Object> and found <Object>?
- Changing kotlin extension function receiver JVM name
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
As Steve already mentioned, you can't utilise java classes in Kotlin/JS.
Think of Kotlin/JS as Typescript. It provides a different syntax to write code that ultimately compiles to JS.
Here are the notable differences of writing Kotlin/JS code vs Kotlin/JVM code
I found reading about Kotlin Multiplatform helped clarify a lot about the capabilities of kotlin.
I know this was not specifically asked, but giving my 2cents to people considering Kotlin/JS (as of Sep'20)
definejavascript functions/classes in kotlin before using them (see here). So if you foresee leveraging a lot of existing javascript modules, it won't be an ideal way forward.