subho
subho

Reputation: 591

Using java functions in scala without import statement

I am new to scala and learning scala. I want to understand how in the below code scala is to interpret java code without any import statement in the scala. As per my knowledge we can use the java code in scala but we have to import the java package.

scala> object Filematcher{
 | private def fileshere = (new java.io.File(".")).listFiles
 | def fileEnding(query: String)=
 | for (file <- fileshere;
 | if file.getName.endsWith(query))
 | yield file
 | }
defined object Filematcher

In this code new java.io.File(".")).listFiles and file.getName.endsWith(query) are java methods

How scala understand it is java code and it is using it without import package statement.

Thanks and Regards,

Upvotes: 0

Views: 85

Answers (1)

Tzach Zohar
Tzach Zohar

Reputation: 37832

In both Java and Scala - if you use the fully-qualified name of a class (e.g. java.io.File, or scala.collection.Seq) you don't need the import. Import statements let the compiler know which class you are referring to without having to write its package:

import java.io.File

val myFile = new File(".")

Lastly - in that respect, there's no difference between calling Java and Scala classes from Scala code - both behave the same. The only difference is that some of Scala's basic classes (e.g. Int, Seq...) are imported by default into every Scala class.

Upvotes: 3

Related Questions