简体   繁体   中英

how java scala interoperability works?

There are many resources available online to understand this, but still am confused.

I'm using Apache spark java api for my work and I can see that some classes are from scala too in spark java jars. I believe both languages bytecode ( classes/code compiled from their respective compilers ) sense is similar to JVM if both java and scala code doing the same thing. ( I guess am right here)

So lets say, I've some code written in scala doing "chow chow chow" work and after compilation its compiled file named as chow.class

I've some code written in java too doing "phow phow phow" work and after compilation its compiled file named as phow.class

doubts:

  1. If I want to use chow.class in java project in eclipse, then If I add this chow.class in my java project, eclipse's decompile would be able to decompile this class so that I could call scala class and see what's there in code and use it too where I want?

  2. How scala's class are bundled in spark java jars? How its being done?

my sense are literally stuck to understand this, can anyone share the example steps to understand this process like the way its happening in spark java api?

I'm using Apache spark java api for my work and I can see that some classes are from scala too in spark java jars.

Sure, because most of Spark is written in Scala .

I believe both languages bytecode (classes/code compiled from their respective compilers) sense is similar to JVM if both java and scala code doing the same thing. ( I guess am right here)

I don't understand what you are trying to say here.
Both languages compile to the JVM bytecode which then is interpreted by a JVM when you run it.

If I want to use chow.class in java project

You just add it to the CLASSPATH in any way you prefer, as well as all the dependencies of that class. Just like when you add any Java dependency to your Java project.

in eclipse

Your IDE is completely irrelevant for this conversation.

then If I add this chow.class in my java project, eclipse's decompile would be able to decompile this class so that I could call scala class and see what's there in code and use it too where I want?

Not sure why you need to decompile it to use it? You don't do that for Java code neither.
Anyways, I believe IntelliJ had some decompiler and conversion tools, not sure if Eclipse has something similar; but again this is not related to anything.

How scala's class are bundled in spark java jars? How its being done?

The same way the Java classes are bundled, the build tool they use ( Maven IIRC) was configured to compile both the Java and Scala source files into .class files and then they packaged all those classes into a single .jar (remember a .jar is basically a .zip )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM