Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How well do Java and Scala work together?

Tags:

java

scala

I have been learning Scala for the past couple of months and now I feel I can start using into real work apart from solving some simple problems. My question here is how well do these two work together?

I have a couple of Java projects which I am working on now. How easy wiil it be to start using scala in them? Are there any gotchas to be aware of? Are there any tutorials or kind of stuff available on doing it? If I want to use Scala in web projects how to do it (other than lift)? All ideas and suggestions welcome.

like image 553
Teja Kantamneni Avatar asked Mar 04 '10 17:03

Teja Kantamneni


2 Answers

In general, using Java libraries from Scala code is easier than using Scala stuff from Java. That tends to happen because Scala has advanced features that are technically accessible in java, but implemented by the scala compiler by generating all kinds of extra classes with mangled names.

If your "main" program is going to be written in java for now, but you want to implement some functionality or modules in scala, you should probably define your modules in terms of Java interfaces. Then, have your scala code implment those interfaces.

Hopefully, you will soon find that you like scala so much that you switch to scala being the main language, calling into your old java code.

like image 141
Peter Recore Avatar answered Sep 30 '22 20:09

Peter Recore


In general, I don't get the premise of mixed-language projects except in extremely specialized cases. Scala is capable (as a language) of expressing everything Java can and more. So why bother "introducing Scala into a Java project"? Why not just write a pure-Scala project?

When I say pure Scala, I'm taking it as read that you may wish to reference a JAR file which was written in Java. But for the vast majority of cases, I see no need to write Java code in a Scala project.

like image 44
oxbow_lakes Avatar answered Sep 30 '22 19:09

oxbow_lakes