Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
Rami Avatar asked Sep 14 '15 09:09

Rami


People also ask

Can Java use Scala libraries?

Scala is designed to work smoothly with Java and Java libraries like JMSL. This is clearly evident in how Scala picks up the same CLASSPATH environment variable Java uses, so if the Java library you wish to use is already there in the CLASSPATH, then you're good to go.

Can we call Scala from Java and Java from Scala?

Scala classes are Java classes, and vice versa. You can call the methods of either language from methods in the other one. You can extend Java classes in Scala, and vice versa. The main limitation is that some Scala features do not have equivalents in Java, for example traits.

Is Scala and Java similar?

Scala is a type-safe JVM (Java Virtual Machine) language launched by Martin Odersky in 2003. It combines both object-oriented and functional programming paradigms into a concise and logical high-level language. Scala is sometimes viewed as an attempt to create a better version of Java, and rightly so.

How do you call a class from Scala in Java?

When you're writing Scala code and need to use a Java collection class, you can just use the class as-is. However, if you want to use the class in a Scala for loop, or want to take advantage of the higher-order functions on the Scala collections classes, you'll want to convert the Java collection to a Scala collection.


2 Answers

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.

like image 170
T.J. Crowder Avatar answered Sep 22 '22 23:09

T.J. Crowder


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>
like image 34
Observer Avatar answered Sep 23 '22 23:09

Observer