Skip to content

Scala 3 Documentation #1861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 171 commits into from
Dec 21, 2020
Merged
Changes from 1 commit
Commits
Show all changes
171 commits
Select commit Hold shift + click to select a range
c6b25b5
Added empty initial files.
alvinj Sep 23, 2020
b39b7a4
This is an early draft to demonstrate my writing style.
alvinj Sep 25, 2020
4af19bb
This is an early draft to demonstrate my writing style.
alvinj Sep 25, 2020
61e26ed
Added more topics while reducing the descriptive text.
alvinj Sep 28, 2020
8a1d644
Worked on the first half of Jonathan's comments.
alvinj Sep 29, 2020
e4f2390
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
97e96f8
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
006dac9
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
e2b11c0
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
5cd4189
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
dd13390
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
1b6f549
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
842f785
Update _overviews/overview/a-taste-of-scala.md
alvinj Sep 29, 2020
b1c49b8
Merge pull request #1 from scalacenter/overview
alvinj Oct 4, 2020
13653e5
New versions of the Taste of Scala, Introduction, and Control Structu…
alvinj Oct 5, 2020
d288738
Added the initial version of the Scala Features document
alvinj Oct 7, 2020
8995017
Initial version of the ‘Interacting With Java’ document.
alvinj Oct 8, 2020
d53ad0e
Initial version of the ‘First Look at Types’ document.
alvinj Oct 8, 2020
8dc7054
Moved some of the Types content to the ‘First Look at Types’ document.
alvinj Oct 8, 2020
ab04eec
Initial version of the ‘Collections Classes and Methods’ document.
alvinj Oct 11, 2020
4e04959
Initial version of the ‘Scala for Java Developers’ document.
alvinj Oct 14, 2020
f0940ac
Initial versions of the ‘Methods’ and ‘Scala for Python Developers’ d…
alvinj Oct 21, 2020
eedcf95
Corrected miscellaneous spelling and syntax errors.
alvinj Oct 22, 2020
ae6cbbd
Adding four new Overview chapters.
alvinj Oct 29, 2020
26eb096
Initial versions of the ‘Main Methods’ and ‘Higher-Order Functions’ d…
alvinj Nov 1, 2020
9ac0fb7
Initial version of the ‘Packaging and Imports’ document.
alvinj Nov 1, 2020
8f3c91e
Renamed the ‘Packaging and Imports’ document.
alvinj Nov 1, 2020
a65106b
Didn’t make this shorter, but improved some areas.
alvinj Nov 2, 2020
f75875d
Initial version of the ‘Functional Programming’ document.
alvinj Nov 9, 2020
607dfd4
Add option to present page as Scala 3 docs
b-studios Nov 24, 2020
98baa72
Start to reorganize contents for inclusion of Scala 3 docs
b-studios Nov 24, 2020
ffc7482
Move language switch all the way to the top
b-studios Nov 25, 2020
07e8f60
Factor into smaller includes
b-studios Nov 25, 2020
b537746
Add new language page offering selection between scala2 and 3
b-studios Nov 25, 2020
76d33d1
Fix language marker in navigation
b-studios Nov 25, 2020
0a6e771
Port macro documentation
b-studios Nov 25, 2020
f95397d
Add stub pages for guides and getting started
b-studios Nov 25, 2020
cf3210b
Update links in navbar
b-studios Nov 25, 2020
0b5f8eb
Hide language specifier on small screens
b-studios Nov 25, 2020
05243e6
Add guides overview for Scala 3
b-studios Nov 25, 2020
e07c7aa
For now put Scala 2 first
b-studios Nov 25, 2020
c926bd8
Added the 'Modeling' chapters, updated the 'Scala for Java/Python' ch…
alvinj Nov 26, 2020
aac6dae
Change permalink structure
b-studios Nov 26, 2020
b2cc305
Add 'Preview' as suggested by Martin
b-studios Nov 26, 2020
b465ac9
Added the 'Contextual Abstractions' chapter as a series of sections.
alvinj Nov 27, 2020
83dba90
Forgot to remove this file.
alvinj Nov 27, 2020
5d968dd
Add scala3 as a default
b-studios Nov 27, 2020
e3dfabf
Add contribution to docs overview
b-studios Nov 27, 2020
ff0a16c
Adding the 'Types' chapter as a series of sections.
alvinj Nov 28, 2020
c81b267
Two new chapters, and two new Types sections.
alvinj Nov 30, 2020
7ca03ed
Don’t need numbers on the filenames any more.
alvinj Dec 2, 2020
d30cc94
Added num, previous-page and next-page to the Front Matter of all pages.
alvinj Dec 4, 2020
0f1e366
Added the Getting Started docs.
alvinj Dec 7, 2020
5485ae4
Make it clear that Scala 3 is not yet released
b-studios Dec 7, 2020
4f33dfb
Merge branch 'overview-docs' into scala3-unification
b-studios Dec 7, 2020
288c312
Move getting started guide
b-studios Dec 7, 2020
f5265f1
Revise getting started
b-studios Dec 7, 2020
7e6d788
Update compatible versions
b-studios Dec 7, 2020
be146ef
Use comment syntax
b-studios Dec 7, 2020
a1c6bf3
move book to scala3-book
b-studios Dec 7, 2020
86479e7
Delete different OS dependent instructions for now
b-studios Dec 7, 2020
0c46b26
Add a link to the reference
b-studios Dec 7, 2020
2120282
Change default to section to only mark chapters
b-studios Dec 8, 2020
a483b1d
Update links in first 4 sections
b-studios Dec 8, 2020
0308998
Updated the JavaScript content and the CSS.
alvinj Dec 8, 2020
d03fa63
Changed 'Overview' references to 'book'. Added a few links.
alvinj Dec 8, 2020
03ae684
Updated page hierarchy and some titles
alvinj Dec 8, 2020
61a2760
Starting to integrate comments
alvinj Dec 8, 2020
5a50882
Working through Jonathan’s comments and related issues
alvinj Dec 10, 2020
b88401f
Do not list h4 in TOC
b-studios Dec 10, 2020
b4e24f4
Make toc part of the section li and fix line-height
b-studios Dec 10, 2020
0636cbf
Disable sliding TOC in the Scala 3 part of the website
b-studios Dec 10, 2020
ce47f5f
Organized the Methods and Functions chapters
alvinj Dec 11, 2020
ca970d3
Quick review of the Methods and Functions chapters
alvinj Dec 11, 2020
d829a6f
Split the Collections chapter into sections
alvinj Dec 12, 2020
f53fe84
Separated the 'Taste of Scala' doc into sections
alvinj Dec 12, 2020
1c2155e
Domain Modeling is more accurate than Data Modeling
alvinj Dec 13, 2020
bdf6d0d
Broke the FP chapter into sections
alvinj Dec 13, 2020
0d7e5ae
A few updates in response to Jonathan’s initial comments
alvinj Dec 13, 2020
60a9b6c
More work on Jonathan’s initial comments
alvinj Dec 14, 2020
e608282
Rephrase intro
b-studios Dec 14, 2020
4e67a51
Add draft of new-in-scala3
b-studios Dec 14, 2020
b7a62d5
Streamline main page to put less focus on Scala 3
b-studios Dec 14, 2020
88111d8
Expand on meta programming
b-studios Dec 14, 2020
0027683
Updated the Scala Tools section, including the 'scala-3-version' setting
alvinj Dec 14, 2020
fb66cf1
Added Toplevel Definitions, made 'Taste of Scala' intro consistent wi…
alvinj Dec 14, 2020
8dc3235
Working through more of Jonathan’s initial comments
alvinj Dec 15, 2020
81c637a
Use globally configured scala 3 versions
b-studios Dec 15, 2020
32360db
Update scala3/index.md
b-studios Dec 15, 2020
43ea987
Update scala3/new-in-scala3.md
b-studios Dec 15, 2020
9ba323a
Remove getting started for now to ease review
b-studios Dec 15, 2020
8232993
Update scala3 macros to be on par with scala3-macro-tutorial
b-studios Dec 15, 2020
308ed17
Revise new-in-scala and add OO section
b-studios Dec 15, 2020
0639958
Add new in Scala 3 to the navigation bar
b-studios Dec 15, 2020
2b3d669
Fix icon headers on landing page
b-studios Dec 15, 2020
3c9ce36
Apply suggestions from code review
b-studios Dec 15, 2020
2e7befa
Apply suggestions from code review
b-studios Dec 15, 2020
6ebc157
Only whitespace fixes
b-studios Dec 15, 2020
c580703
Incorporate @sjrd's suggestions for the scala-for-js-devs section
b-studios Dec 15, 2020
a4a9ba6
Fix links in the contextual abstractions chapter
b-studios Dec 15, 2020
fbb2f50
Update given syntax as pointed out by @bishabosha
b-studios Dec 15, 2020
c810c68
Two more given syntax updates
b-studios Dec 15, 2020
7490ba9
Apply suggestions from code review by @vincenzobaz
b-studios Dec 15, 2020
928cf03
Apply suggestions from code review by @adpi2
b-studios Dec 15, 2020
03f2652
Apply suggestions from code review by @WojciechMazur
b-studios Dec 15, 2020
99255a6
Apply suggestions from code review by @tgodzik
b-studios Dec 15, 2020
53a7e7d
Apply suggestions from code review @bishabosha
b-studios Dec 15, 2020
cbe5d4d
Made the Getting Started page more concise
alvinj Dec 16, 2020
2528b49
Sketch Scala 3 hierarchy with Matchable
b-studios Dec 16, 2020
2dd22d3
Revise first-look-at-types
b-studios Dec 16, 2020
6e17bf0
Revise control-structures
b-studios Dec 16, 2020
f77c931
Revise modeling intro and tools
b-studios Dec 16, 2020
a0f7cdd
Add missing code fences
b-studios Dec 16, 2020
c2b7aec
Revise methods sections
b-studios Dec 16, 2020
bc00b1d
Streamline presentation of files a bit
b-studios Dec 16, 2020
ffbd2c1
Use import suggestions paragraph from @julienrf
b-studios Dec 16, 2020
b5cc84e
Revise oo design
b-studios Dec 16, 2020
3ab41f9
Mostly responded to comments by @tgodzik
alvinj Dec 17, 2020
2fecc4a
Resolved my own TODO items, or made them Jekyll comments
alvinj Dec 17, 2020
4b26c1f
Resolved most comments from @vincenzobaz and @b-studios
alvinj Dec 17, 2020
e95ed18
Replaced 'class' with 'type', per @vincenzobaz comments in other sect…
alvinj Dec 17, 2020
1fb1faa
Reviewed the OOP page
alvinj Dec 17, 2020
3a2e890
Revise FP modeling
b-studios Dec 17, 2020
a02c100
Revise type section up to Union types
b-studios Dec 17, 2020
3c831d6
Revise enum section
b-studios Dec 17, 2020
f46157f
Revise type class section
b-studios Dec 17, 2020
93f9c10
Fixing minor typos
b-studios Dec 17, 2020
76e8465
Revise variance section
b-studios Dec 17, 2020
c1caf6c
Drop those from the list since we do cover them (at least briefly)
b-studios Dec 17, 2020
74a45c8
Revise opaque types
b-studios Dec 17, 2020
7926c00
Add a quick example why we say 'leaky'
b-studios Dec 17, 2020
607410f
Updates for @vinz’s comments
alvinj Dec 17, 2020
ac0f935
Delete all HTML comments before going live
alvinj Dec 17, 2020
63533a2
Removed the Metaprogramming chapter and renumbered the files
alvinj Dec 17, 2020
b67dcaa
Responded to @vincenzobaz’s comments
alvinj Dec 18, 2020
42047b6
Responded to @sjrd’s comments
alvinj Dec 18, 2020
f0531f6
Seems like a good place to reference the Migration Guide
alvinj Dec 18, 2020
5a29693
Responding to more comments
alvinj Dec 18, 2020
3c56556
Reviewed the 'types' pages
alvinj Dec 18, 2020
ce4967b
Drop the links to Scala 2 and Scala 3 pages and integrate on landing …
b-studios Dec 18, 2020
87dde8a
Add a clearly visible disclaimer on every page
b-studios Dec 18, 2020
693cf3d
Slightly revise the getting started page
b-studios Dec 18, 2020
7a5a4f4
Remove text underline from docs link in the nav bar
b-studios Dec 18, 2020
5a01937
Fix multiple highlighted headlines in TOC
b-studios Dec 18, 2020
f0cdee4
Revise dependent function types
b-studios Dec 18, 2020
ac860f9
Revise contextual abstractions
b-studios Dec 18, 2020
75a3507
Minor fixes
b-studios Dec 18, 2020
042546e
Updates per @vincenzobaz’s comments
alvinj Dec 18, 2020
18edb35
Moved Type Classes to just after Givens
alvinj Dec 18, 2020
a077f63
Use current milestone
b-studios Dec 18, 2020
dceb41b
Update macros
b-studios Dec 18, 2020
8159550
All paragraphs are now separate lines to simplify PRs
alvinj Dec 18, 2020
f2c57cb
Added line breaks for easier comments and PRs
alvinj Dec 18, 2020
064782e
Use a consistent style on all blockquotes
alvinj Dec 18, 2020
371ae2c
Resolved ‘See the Reference docs’ lines that had nothing to link to
alvinj Dec 19, 2020
f378c0f
Fixed broken links
alvinj Dec 19, 2020
ec7077f
Reduced ‘Summary’ titles after recent ToC fix
alvinj Dec 19, 2020
a9a0ac3
Removed side-by-side tables
alvinj Dec 19, 2020
8cced66
First attempt at improving code block readability
alvinj Dec 19, 2020
ad3082e
Got the desired look using only CSS
alvinj Dec 20, 2020
f111c93
Updated the JavaScript page, put the CSS in one file
alvinj Dec 20, 2020
f6a545b
Updated the Python page, and the CSS
alvinj Dec 21, 2020
6b470ff
Working to resolve the Pure Function definition
alvinj Dec 21, 2020
d9cff8d
Fix link in navigation
b-studios Dec 21, 2020
be74175
Fix some links in macros tutorial
b-studios Dec 21, 2020
854b37d
Fix some more links
b-studios Dec 21, 2020
707ed21
Comment it out, if it is outdated
b-studios Dec 21, 2020
4c52788
Fix a few more broken links
b-studios Dec 21, 2020
5769941
Fix remaining broken links
b-studios Dec 21, 2020
1c02731
Change link of migration status
b-studios Dec 21, 2020
b3d04af
Also update migration status on contribute to docs page
b-studios Dec 21, 2020
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial version of the ‘Interacting With Java’ document.
  • Loading branch information
alvinj committed Oct 8, 2020
commit 8995017de99b4745ba791d3491643fd45d272d58
368 changes: 368 additions & 0 deletions _overviews/overview/interacting-with-java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,368 @@
---
title: Interacting with Java
description: This page demonstrates how Scala code can interact with Java, and how Java code can interact with Scala code.
---


## Introduction

This section looks at how to use Java code in Scala, and the opposite, how to use Scala code in Java.

In general, using Java code in Scala is pretty seamless. There are only a few points where you’ll want to use Scala utilities to convert Java concepts to Scala, including:

- Java collections classes
- The Java `Optional` class

Similarly, if you’re writing Java code and want to use Scala concepts, you’ll want to convert Scala collections and the Scala `Option` class.

These following sections demonstrate the most common conversions you’ll need:

- How to use Java collections in Scala
- How to use Java `Optional` in Scala
- Extending Java interfaces in Scala
- How to use Scala collections in Java
- How to use Scala `Option` in Java
- How to use Scala traits in Java
- How to handle Scala methods that throw exceptions in Java code
- How to use Scala varargs parameters in Java
- Create alternate names to use Scala methods in Java

Note that the Java examples in this section assume that you’re using Java 11 or newer.



## How to use Java collections in Scala

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.

Here’s an example of how this works. Given this Java `ArrayList`:

<!-- verified -->
```java
// java
public class JavaClass {
public static List<String> getStrings() {
return new ArrayList<String>(List.of("a", "b", "c"));
}
}
```

You can convert that Java list to a Scala `Seq`, using the conversion utilities in the Scala _scala.jdk.CollectionConverters_ package:

<!-- verified -->
```scala
// scala
import scala.jdk.CollectionConverters._
import java.util.List

def testList =
println("Using a Java List in Scala")
val javaList: java.util.List[String] = JavaClass.getStrings()
val scalaSeq: Seq[String] = javaList.asScala.toSeq
scalaSeq.foreach(println)
for s <- scalaSeq do println(s)
```

Of course that code can be shortened, but the individual steps are shown here to demonstrate exactly how the conversion process works.



## How to use Java `Optional` in Scala

When you need to use the Java `Optional` class in your Scala code, import the _scala.jdk.OptionConverters_ object, and then use the `toScala` method to convert the `Optional` value to a Scala `Option`.

To demonstrate this, here’s a Java class with two `Optional<String>` values, one containing a string and the other one empty:

<!-- verified -->
```java
// java
import java.util.Optional;

public class JavaClass {
static Optional<String> oString = Optional.of("foo");
static Optional<String> oEmptyString = Optional.empty();
}
```

Now in your Scala code you can access those fields. If you just access them directly, they’ll both be `Optional` values:

<!-- verified -->
```scala
// scala
import java.util.Optional

val optionalString = JavaClass.oString // Optional[foo]
val eOptionalString = JavaClass.oEmptyString // Optional.empty
```

But by using the _scala.jdk.OptionConverters_ methods, you can convert them to Scala `Option` values:

<!-- verified -->
```scala
import java.util.Optional
import scala.jdk.OptionConverters._

val optionalString = JavaClass.oString // Optional[foo]
val optionString = optionalString.toScala // Some(foo)

val eOptionalString = JavaClass.oEmptyString // Optional.empty
val eOptionString = eOptionalString.toScala // None
```



## Extending Java interfaces in Scala

If you need to use Java interfaces in your Scala code, extend them just as though they are Scala traits. For example, given these three Java interfaces:

<!-- verified -->
```java
// java
interface Animal {
void speak();
}

interface Wagging {
void wag();
}

interface Running {
// an implemented method
default void run() {
System.out.println("I’m running");
}
}
```

you can create a `Dog` class in Scala just as though you were using traits. All you have to do is implement the `speak` and `wag` methods:

<!-- verified -->
```scala
// scala
class Dog extends Animal, Wagging, Running:
def speak = println("Woof")
def wag = println("Tail is wagging")

@main def useJavaInterfaceInScala =
val d = new Dog
d.speak
d.wag
```



## How to use Scala collections in Java

When you need to use a Scala collection class in your Java code, use the methods of Scala’s _scala.javaapi.CollectionConverters_ object in your Java code to make the conversions work. For example, if you have a `List[String]` like this in a Scala class:

<!-- verified -->
```scala
// scala
class ScalaClass:
val strings = List("a", "b")
```

You can access that Scala `List` in your Java code like this:

<!-- verified -->
```java
// java
import scala.jdk.javaapi.CollectionConverters;

// create an instance of the Scala class
ScalaClass sc = new ScalaClass();

// access the `strings` field as `sc.strings()`
scala.collection.immutable.List<String> xs = sc.strings();

// convert the Scala `List` a Java `List<String>`
java.util.List<String> listOfStrings = CollectionConverters.asJava(xs);
listOfStrings.forEach(System.out::println);
```

That code can be shortened, but the full steps are shown to demonstrate how the process works. Here are a few things to notice in that code:

- In your Java code, you create an instance of `ScalaClass` just like an instance of a Java class
- `ScalaClass` has a field named `strings`, but from Java you access that field as a method, i.e., as `sc.strings()`



## How to use Scala `Option` in Java

When you need to use a Scala `Option` in your Java code, you can convert the `Option` to a Java `Optional` value using the `toJava` method of the Scala _scala.jdk.javaapi.OptionConverters_ object.

To demonstrate this, create a Scala class with two `Option[String]` values, one containing a string and the other one empty:

<!-- verified -->
```scala
// scala
object ScalaObject:
val someString = Option("foo")
val noneString: Option[String] = None
```

Then in your Java code, convert those `Option[String]` values into `java.util.Optional[String]` using the `toJava` method from the _scala.jdk.javaapi.OptionConverters_ object:

<!-- verified -->
```java
// java
import java.util.Optional;
import static scala.jdk.javaapi.OptionConverters.toJava;

public class JUseScalaOptionInJava {
public static void main(String[] args) {
Optional<String> stringSome = toJava(ScalaObject.someString()); // Optional[foo]
Optional<String> stringNone = toJava(ScalaObject.noneString()); // Optional.empty
System.out.printf("stringSome = %s\n", stringSome);
System.out.printf("stringNone = %s\n", stringNone);
}
}
```

The two Scala `Option` fields are now available as Java `Optional` values.



## How to use Scala traits in Java

With Java 11 you can use a Scala trait just like a Java interface, even if the trait has implemented methods. For example, given these two Scala traits, one with an implemented method and one with only an interface:

<!-- verified -->
```scala
// scala
trait ScalaAddTrait:
def sum(x: Int, y: Int) = x + y // implemented

trait ScalaMultiplyTrait:
def multiply(x: Int, y: Int): Int // abstract
```

A Java class can implement both of those interfaces, and implement the `multiply` method:

<!-- verified -->
```java
// java
class JavaMath implements ScalaAddTrait, ScalaMultiplyTrait {
public int multiply(int a, int b) {
return a * b;
}
}

JavaMath jm = new JavaMath();
System.out.println(jm.sum(3,4)); // 7
System.out.println(jm.multiply(3,4)); // 12
```



## How to handle Scala methods that throw exceptions in Java code

When you’re writing Scala code using Scala programming idioms, you’ll never write a method that throws an exception. But if for soem reason you have a Scala method that does throw an exception, and you want Java developers to be able to use that method, add the `@throws` annotation to your Scala method so Java consumers will know the exceptions they can throw.

For example, this Scala `exceptionThrower` method is annotated to declare that it throws an `Exception`:

<!-- verified -->
```scala
// scala
object SExceptionThrower:
@throws(classOf[Exception])
def exceptionThrower =
throw new Exception("Idiomatic Scala methods don’t throw exceptions")
```

As a result, you’ll need to handle the exception in your Java code. For instance, this code won’t compile because I don’t handle the exception:

<!-- verified -->
```java
// java: won’t compile because the exception isn’t handled
public class ScalaExceptionsInJava {
public static void main(String[] args) {
SExceptionThrower.exceptionThrower();
}
}
```

The compiler gives this error:

....
[error] ScalaExceptionsInJava: unreported exception java.lang.Exception;
must be caught or declared to be thrown
[error] SExceptionThrower.exceptionThrower()
....

This is good — it’s what you want: the annotation tells the Java compiler that `exceptionThrower` can throw an exception. Now when you’re writing Java code you must handle the exception with a `try` block or declare that your Java method throws an exception.

Conversely, if you leave the annotation off of the Scala `exceptionThrower` method, the Java code _will compile_. This is probably not what you want, because the Java code may not account for the Scala method throwing the exception.



## How to use Scala varargs parameters in Java

When a Scala method has a varargs parameter and you want to use that method in Java, mark the Scala method with the `@varargs` annotation. For example, the `printAll` method in this Scala class declares a `String*` varargs field, and the method is marked with the `@varargs` annotation:

<!-- verified -->
```scala
// scala
import scala.annotation.varargs

object VarargsPrinter:
@varargs def printAll(args: String*): Unit = args.foreach(println)
```

Because `printAll` is declared with the `@varargs` annotation, it can be called from a Java program with a variable number of parameters, as shown in this example:

<!-- verified -->
```scala
// java
public class JVarargs {
public static void main(String[] args) {
VarargsPrinter.printAll("Hello", "world");
}
}

```

When this code is run, it results in the following output:

....
Hello
world
....



## Create alternate names to use Scala methods in Java

In Scala you might want to create a method name using a symbolic character:

<!-- verified -->
```scala
def +(a: Int, b: Int) = a + b
```

That method name won’t work well in Java, but what you can do in Scala 3 is provide an “alternate” name for the method — an alias — that will work in Java:

<!-- verified -->
```scala
import scala.annotation.alpha

class Adder:
@alpha("add") def +(a: Int, b: Int) = a + b
```

Now in your Java code you can use the aliased method name `add`:

```scala
var adder = new Adder();
int x = adder.add(1,1);
System.out.printf("x = %d\n", x);
```



## More information

This section highlights some of the main topics to know when integrating Scala and Java code bases. For more details, including a few other topics not covered here, see the _Interacting with Java_ section in the Reference documentation.