subho subho - 2 months ago 3x
Scala Question

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".")).listFiles
| def fileEnding(query: String)=
| for (file <- fileshere;
| if file.getName.endsWith(query))
| yield file
| }
defined object Filematcher

In this code

are java methods

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

Thanks and Regards,


In both Java and Scala - if you use the fully-qualified name of a class (e.g., 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:


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.