Scala is a very powerful programming language that has full support for functional programming and besides provides programming with the widely-used object-oriented paradigm. After you delve into the details of the Scala and learn, you begin to search for the similar features and abilities in your favorite programming language.
Scala source code is intended to be compiled to Java bytecode. So that the resulting executable code runs on a Java virtual machine( JVM ). In the recent years, JVM-based languages like Scala, Groovy, Clojure... are becoming more popular and they provide more capabilities and characteristics than Java itself has. So I think, it triggers the Java to enhance and polish up with extra features. For example, in the Java8, lambda expressions allow us to use functional programming notations in Java. But in the other JVM languages, similar features already exist for years.
Luckily, if you are Java developer, you have a chance to use Scala with Java and make polyglot programming. Scala is interoperable with Java and Java libraries may be used directly in Scala code. In this post, I will explain how to code in Scala and Java in the same project in the Eclipse IDE.
First of all, you should install the "Scala IDE" plug-in in your Eclipse. In a basic project, I will put Java and Scala codes in the project and use Java code in the Scala code and likewise vice versa. I will illustrate it step by step.
1) Create Project
In the Eclipse, create a new "Scala Project" going via "File" -> "New" -> "Scala Project". Create "src/main/java" source folder for Java codes and "src/main/scala" source folder for Scala codes. After applying them, the project layout in the Eclipse is seemed like this:
2) Create Packages
Create a Java Package in the "src/main/java" source folder and a Scala Package Object in the "src/main/scala" source folder. I name them "polyglot.java" and "polyglot.scala", respectively:
3) Add a Scala Class and a Java Main method
I create a "RationalNumber" scala class into the scala package. This class includes a "constructor" that gets the numerator and denominator parts of the rational number, "add" and "toString" functions.
class RationalNumber( num : Int, dem : Int ) {
def numerator = num
def denominator = dem
def add( added : RationalNumber ) = new RationalNumber( this.numerator *
added.denominator + added.numerator * this.denominator, this.denominator *
added.denominator )
override def toString = this.numerator + "/" + this.denominator
}
I add a Java class with a "main" method to start the execution of the code. The main method imports the "RationalNumber" Scala class. As you see, in the Java code we can use a Scala code.
// import the Scala class here.
import polyglot.scala.RationalNumber;
public class TestMain {
public static void main(String[] args) {
RationalNumber r1 = new RationalNumber( 3, 5 );
System.out.println( r1 );
System.out.println( r1.add( new RationalNumber( 1, 3 ) ) );
}
}
4) Import Java Code in the Scala Code
We imported the Scala code in the Java code. Now on the contrary, we use Java code in the Scala code. In the "RationalNumberCalculator" Java class, there is an implementation of the rational number subtraction operation:
//Import the Scala code.
import polyglot.scala.RationalNumber;
public class RationalNumberCalculator {
public RationalNumber sub( final RationalNumber minuend, final RationalNumber subtrahend ) {
return new RationalNumber( minuend.numerator() * subtrahend.denominator() -
subtrahend.numerator() * minuend.denominator(),
minuend.denominator() * subtrahend.denominator() );
}
}
We revise the "RationalNumber" Scala class and import the "RationalNumberCalculator" Java class. It is Java code usage in the Scala code sample.
// Import Java code
import polyglot.java.RationalNumberCalculator
class RationalNumber( num : Int, dem : Int ) {
def numerator = num
def denominator = dem
// Instantiate a Java class as a Scala variable.
val rationalNumberCalculator = new RationalNumberCalculator()
def add( added : RationalNumber ) = new RationalNumber( this.numerator *
added.denominator + added.numerator * this.denominator, this.denominator *
added.denominator )
override def toString = this.numerator + "/" + this.denominator
// Define a Scala function that invokes a Java method
def sub( subNumber : RationalNumber ) = rationalNumberCalculator.sub(this, subNumber)
}
In the main method, we can also exemplify the "sub" function call:
// import the Scala class here.
import polyglot.scala.RationalNumber;
public class TestMain {
public static void main(String[] args) {
RationalNumber r1 = new RationalNumber( 3, 5 );
System.out.println( r1 );
System.out.println( r1.add( new RationalNumber( 1, 3 ) ) );
System.out.println( r1.sub( new RationalNumber( 1, 3 ) ) );
}
}
Finally, we obtain the output of the Main method execution like below:
14/15
4/15
Conclusion
We can use Java and Scala programming languages interchangeably in the same project. We can download full source code and the Eclipse project here: ScalaWithJava.zip