Scala Guide for Java Developers

Source: Internet
Author: User
Tags foreach

Recently, readers ' feedback made me realize that in the course of making this series I missed an important aspect of Scala's language: The Scala package and access modifier features. So before studying the functional elements of the language apply mechanism, I will introduce the package and access modifiers first.

Packaged

To help isolate code so that it does not conflict with each other, Java™ Code provides package keywords, creating a lexical namespace for declaring classes. In essence, the class Foo is placed in the name Com.tedneward.util package to modify the formal class name to Com.tedneward.util.Foo; Likewise, the class must be referenced by that method. If not, Java programmers will quickly point out that they will import the package and avoid the hassle of typing a formal name. This is true, but this only means that the work of referencing a class according to the official name is done by the compiler and bytecode. A quick glance at JAVAP's output will be clear.

However, there are several special requirements for packages in the Java language: Be sure to declare the package at the top of the. Java file where the package is acting (this raises a very serious language problem when applying the annotation to a package), and the scope of the declaration is the entire file. This means that two classes that are tightly coupled across packages must be separated across files, which makes the tight coupling between them easy to ignore.

Scala's approach to packaging is somewhat different, combining the declaration method of the Java language with the scope (scoping) method of C #. With this in mind, Java developers can use traditional Java methods and place package declarations at the top of the. Scala file, just like normal Java classes; The scope of the package declaration is the entire file, as in Java code. Scala developers can use the Scala package "(scoping) scoping" method to limit the scope of package statements with curly braces, as shown in Listing 1:

Listing 1. Simplified packaging

package com
{
 package tedneward
 {
  package scala
  {
   package demonstration
   {
    object App
    {
     def main(args : Array[String]) : Unit =
     {
      System.out.println("Howdy, from packaged code!")
      args.foreach((i) => System.out.println("Got " + i) )
     }
    }
   }
  }
 }
}

This code effectively declares the class App, or rather a single class called COM.TEDNEWARD.SCALA.DEMONSTRATION.APP. Note that Scala also allows dots to separate the package names, so the code in Listing 1 can be more concise, as shown in Listing 2:

Listing 2. Simplified Packaging (redux)

package com.tedneward.scala.demonstration
{
  object App
  {
    def main(args : Array[String]) : Unit =
    {
     System.out.println("Howdy, from packaged code!")
     args.foreach((i) => System.out.println("Got " + i) )
    }
  }
}

Which style looks all right, because they all compile the same code construct (Scala will continue to compile and build the. class file in the subdirectory of the declaration package as Javac).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.