Scala Self-Study Diary (3)-write the first Hello world!

Source: Internet
Author: User
Tags dateformat locale

In the previous chapter we introduced and then used the interpreter to write simple Scala code, and here's how to write a Hello word! using eclipse

Open Eclipse. Create a new Scala Project. The first new words should be in other, name casually, then create a new Scala Object named "HelloWorld".

Unlike Scala and Java, the name of a file does not have to be the same as the name of object.

The code is as follows:

Package Com.scala.demo

Object HelloWorld {
  def main (args:array[string])
  {
    println ("Hello world!")
  }

}
After running, it will show:

Hello world!
See it! It's very simple, there is a place in the last chapter is involved in, look at the last chapter!

http://blog.csdn.net/y3wegy/article/details/8024412

Add in the following:

Scala is compatible, so Scala and Java can be accessed from one another,

One of the strengths of Scala is that it's easy to interact with existing Java code, all classes in Java.lang have been imported automatically, and other classes need to explicitly declare imports.
Let's look at the demo code. We would like to format the date, such as the format of the country of use.
The Java class Library defines a list of useful classes, such as date and DateFormat. Because Scala can interact well with Java, we don't need to implement equivalent code in the Scala class library, just direct Java-related class import.

Package Com.scala.demo
Import java.util.{ Date, Locale}
import java.text.DateFormat
import java.text.dateformat._

object Localdate {
  def main ( Args:array[string])
  {
      val now=new Date
      val df=getdateinstance (long,locale.france)
      println (DF Format now-  //==df.format (now) ... Strange Syntax
  }
}
Scala's import statements look very similar to Java's, but they are more powerful. You can use curly braces to import multiple classes in the same package, just as the first line in the code above does. Another difference is that when you import all the classes or symbols in a package, you should use an underscore (_) instead of an asterisk (*). This is because the asterisk is a valid identifier (for example, as a method name) in Scala. This example we will meet later.
The import statement in the third row imports all members of the DataFormat class, which allows static method getdateinstance and static variable long to be referenced directly.
In the main function, we first set up a date instance of Java. This instance will contain the current time by default. Next we define the date format using the static function getdateinstance that we just imported. Finally, we printed the date with the Datafotmat format.
The last line of code shows an interesting syntax for Scala: a function with only one parameter can be represented by an expression such as the following:
DF Format Now
This is the short, concise expression of the following.
Df.format (now)
This appears to be a grammatical detail, but it leads to an important consequence that we will explain in the next section.
In addition, we should also note that Scala can directly inherit or implement interfaces and classes in Java.
Having seen the Scala examples above and the way Scala interacts with Java, the following is an introduction to Scala's objects.

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.