Code Examples for

Programming in Scala, Fourth Edition

Return to chapter index

31 Combining Scala and Java

Sample run of chapter's interpreter examples

31.1 Using Scala from Java


// In file combining-scala-and-java/App.scala object App { def main(args: Array[String]) = { println("Hello, world!") } }
$ javap App$ Compiled from "App.scala" public final class App$ { public static final App$ MODULE$; public static {}; public void main(java.lang.String[]); }
$ javap App Compiled from "App.scala" public class App { public static void main(java.lang.String[]); public App(); }

31.2 Annotations


// Java serial version marker private final static long SerialVersionUID = 1234L
// In file combining-scala-and-java/Reader.scala import java.io._ class Reader(fname: String) { private val in = new BufferedReader(new FileReader(fname)) @throws(classOf[IOException]) def read() = in.read() }
$ javap Reader Compiled from "Reader.scala" public class Reader { public int read() throws java.io.IOException; public Reader(java.lang.String); }
// In file combining-scala-and-java/SetTest.scala import org.junit.Test import org.junit.Assert.assertEquals class SetTest { @Test def testMultiAdd = { val set = Set() + 1 + 2 + 3 + 1 + 2 + 3 assertEquals(3, set.size) } }
$ scala -cp junit-4.3.1.jar:. org.junit.runner.JUnitCore SetTest JUnit version 4.3.1 . Time: 0.023 OK (1 test)
// In file combining-scala-and-java/Ignore.java import java.lang.annotation.*; // This is Java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Ignore { }
// In file combining-scala-and-java/Tests.scala object Tests { @Ignore def testData = List(0, 1, -1, 5, -5) def test1 = { assert(testData == (testData.head :: testData.tail)) } def test2 = { assert(testData.contains(testData.head)) } }
// In file combining-scala-and-java/FindTests.scala for { method <- Tests.getClass.getMethods if method.getName.startsWith("test") if method.getAnnotation(classOf[Ignore]) == null } { println("found a test method: " + method) }
$ javac Ignore.java $ scalac Tests.scala $ scalac FindTests.scala $ scala FindTests found a test method: public void Tests$.test2() found a test method: public void Tests$.test1()

31.3 Wildcard types


// In file combining-scala-and-java/Wild.java // This is a Java class with wildcards import java.util.*; public class Wild { public Collection<?> contents() { Collection<String> stuff = new Vector<String>(); stuff.add("a"); stuff.add("b"); stuff.add("see"); return stuff; } }
scala> val contents = (new Wild).contents contents: java.util.Collection[_] = [a, b, see]
scala> contents.size() res0: Int = 3
import scala.collection.mutable val iter = (new Wild).contents.iterator val set = mutable.Set.empty[???] // what type goes here? while (iter.hasMore) set += iter.next()
// In file combining-scala-and-java/UseWild.scala import scala.collection.mutable import java.util.Collection abstract class SetAndType { type Elem val set: mutable.Set[Elem] } def javaSet2ScalaSet[T](jset: Collection[T]): SetAndType = { val sset = mutable.Set.empty[T] // now T can be named! val iter = jset.iterator while (iter.hasNext) sset += iter.next() new SetAndType { type Elem = T val set = sset } }

31.4 Compiling Scala and Java together


$ scalac -d bin InventoryAnalysis.scala InventoryItem.java \ Inventory.java $ javac -cp bin -d bin Inventory.java InventoryItem.java \ InventoryManagement.java $ scala -cp bin InventoryManagement Most expensive item = sprocket($4.99)

31.5 Java 8 integration


JButton button = new JButton(); // This is Java button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("pressed!"); } } );
JButton button = new JButton(); // This is Java 8 button.addActionListener( event -> System.out.println("pressed!") );
val button = new JButton button.addActionListener( _ => println("pressed!") )
scala> trait Increaser { def increase(i: Int): Int } defined trait Increaser
scala> def increaseOne(increaser: Increaser): Int = increaser.increase(1) increaseOne: (increaser: Increaser)Int
scala> increaseOne( new Increaser { def increase(i: Int): Int = i + 7 } ) res0: Int = 8
scala> increaseOne(i => i + 7) // Scala 2.12 res1: Int = 8
scala> import java.util.function.IntUnaryOperator import java.util.function.IntUnaryOperator scala> import java.util.Arrays import java.util.Arrays scala> val stream = Arrays.stream(Array(1, 2, 3)) stream: java.util.stream.IntStream = ... scala> stream.map( new IntUnaryOperator { def applyAsInt(i: Int): Int = i + 1 } ).toArray res3: Array[Int] = Array(2, 3, 4)
scala> val stream = Arrays.stream(Array(1, 2, 3)) stream: java.util.stream.IntStream = ... scala> stream.map(i => i + 1).toArray // Scala 2.12 res4: Array[Int] = Array(2, 3, 4)
scala> val f = (i: Int) => i + 1 f: Int => Int = ...
scala> val stream = Arrays.stream(Array(1, 2, 3)) stream: java.util.stream.IntStream = ... scala> stream.map(f).toArray <console>:16: error: type mismatch; found : Int => Int required: java.util.function.IntUnaryOperator stream.map(f).toArray ^
scala> stream.map(i => f(i)).toArray res5: Array[Int] = Array(2, 3, 4)
scala> val f: IntUnaryOperator = i => i + 1 f: java.util.function.IntUnaryOperator = ... scala> val stream = Arrays.stream(Array(1, 2, 3)) stream: java.util.stream.IntStream = ... scala> stream.map(f).toArray res6: Array[Int] = Array(2, 3, 4)

31.6 Conclusion

For more information about Programming in Scala, Fourth Edition (the "Stairway Book"), please visit:

http://www.artima.com/shop/programming_in_scala_4ed

and:

http://booksites.artima.com/programming_in_scala_4ed

Copyright © 2007-2019 Artima, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.