Rami
Rami

Reputation: 8314

Can we write Scala code inside a Java program?

Sorry for the basic question, I have already a large software written in Java (I am using Eclipse Mars for editing) and I would like to change some classes in it and use Scala instead of Java, is such a thing possible?

Upvotes: 10

Views: 7861

Answers (2)

Observer
Observer

Reputation: 710

Add a subfolder into src named scala and put your code there. You'll have to rewrite the class in Scala completely. After that add Scala compilation plugin to mvn.pom or ask the google for your build system.

<plugin>
  <groupId>net.alchim31.maven</groupId>
  <artifactId>scala-maven-plugin</artifactId>
  <version>3.2.0</version>
  <configuration>
    <charset>${project.build.sourceEncoding}</charset>
    <recompileMode>modified-only</recompileMode>
    <scalaCompatVersion>2.11</scalaCompatVersion>
    <scalaVersion>2.11.5</scalaVersion>
    <jvmArgs>
      <jvmArg>-Xmx1024m</jvmArg>
      <jvmArg>-DpackageLinkDefs=file://${project.build.directory}/packageLinkDefs.properties</jvmArg>
    </jvmArgs>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>testCompile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1075337

Right there on the scala-lang.org front page:

SEAMLESS JAVA INTEROP - Scala runs on the JVM, so Java and Scala stacks can be freely mixed for totally seamless integration.

And if you click it, it gives you this further information:

Combine Scala and Java seamlessly

Scala classes are ultimately JVM classes. You can create Java objects, call their methods and inherit from Java classes transparently from Scala. Similarly, Java code can reference Scala classes and objects.

In this example, the Scala class Author implements the Java interface Comparable and works with Java Files. The Java code uses a method from the companion object Author, and accesses fields of the Author class. It also uses JavaConversions to convert between Scala collections and Java collections.

With this example:

Author.scala:

class Author(val firstName: String,
    val lastName: String) extends Comparable[Author] {
  override def compareTo(that: Author) = {
    val lastNameComp = this.lastName compareTo that.lastName
    if (lastNameComp != 0) lastNameComp
    else this.firstName compareTo that.firstName
  }
}
object Author {
  def loadAuthorsFromFile(file: java.io.File): List[Author] = ???
}

App.java:

import static scala.collection.JavaConversions.asJavaCollection;
public class App {
    public List<Author> loadAuthorsFromFile(File file) {
        return new ArrayList<Author>(asJavaCollection(
            Author.loadAuthorsFromFile(file)));
    }
    public void sortAuthors(List<Author> authors) {
        Collections.sort(authors);
    }
    public void displaySortedAuthors(File file) {
        List<Author> authors = loadAuthorsFromFile(file);
        sortAuthors(authors);
        for (Author author : authors) {
            System.out.println(
                author.lastName() + ", " + author.firstName());
        }
    }
}

Your example is probably the other way around, using Java classes in a Scala app, but since at the end of the day it's just JVM classes (and the example above uses Java classes, like java.io.File, in the Scala code), it's the same thing.

Upvotes: 6

Related Questions