Import statements in Scala

One more baby step into Scala, in this second of a series of posts. Import statements.

Scala’s import statements are very similar to Java’s, but with some small differences that allow you to write more concise code. Take a look at the following examples:

1. _ instead of *

Java:

import java.util.*;

Scala:

import scala.util._

2. Importing multiple classes from the same package

Java:

import java.util.Date;
import java.util.Collection;
import java.util.List;

Scala:

import java.util.{Date, Collection, List}

3. Renaming

Java:

// can't be done

Scala:

import java.util.{Collection => JavaCollection}

Although I used Java classes in almost all the examples above, you can of course use the same syntax for all the Scala classes as well.

That’s all for now! What do you want to read next about Scala? Leave a comment!

Advertisements
This entry was posted in java, scala and tagged , , , . Bookmark the permalink.

One Response to Import statements in Scala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s