Scala interacts with Java

Source: Internet
Author: User
Tags dateformat locale

one of Scala's strengths is that it can be very simple to interact with existing Java code, and all classes in Java.lang have been automatically imported, while others need to explicitly declare imports.
Let's take a look at the demo code. We want to format the date, for example the format of the country of use.
The Java Class Library defines a series of useful classes, such as date and DateFormat. Since Scala has a good interaction with Java, we don't need to implement the equivalent code in the Scala class library, but simply import the relevant Java classes:
import Java.util. {Date, Locale}
Import Java.text.DateFormat Import Java.text.dateformat._ object Frenchdate {def main (args:array[string]) {Val Now = new Date val df = getdateinstance (LONG, locale.france) println (DF Format Now)}}
Scala's import statement looks very similar to Java's, but it's more powerful. You can use curly braces to import multiple classes in the same package, just like the first line in the code above. 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 in Scala (for example, as a method name). This example we will encounter later.
The import statement in the third row imports all the members in the DataFormat class, which makes the static method getdateinstance and the static variable long can be directly referenced.
in the main function, we first set up a Java date instance. This instance will contain the current time by default. Next we define the date format using the static function getdateinstance just imported. Finally, we will print out the date formatted with Datafotmat. 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
In fact, this is the simple wording of this lengthy expression .
Df.format (now)
this looks like a syntax detail, but it causes 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.

Scala interacts with Java

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.