Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Hello World in Scala

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to write Hello World in Scala". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Hello World in Scala.

The name of the Scala language comes from "extensible language", and Scala is a mixed language of function objects. It can run on the Java platform, and the Scala program will be compiled into JVM bytecode. Their execution performance is usually the same as that of Java programs. The Scala code can call the Java method, access the Java field, inherit from the Java class and implement the Java interface.

Download and install Scala

You can download the Scala package from the Scala home page: http://www.scala-lang.org/. So far, the distribution of * * is 2.7.5-final, and it also provides plug-ins for IDE. The plug-ins for Eclipse's Scala can be downloaded from http://www.scala-lang.org/node/94, or you can download them through Eclipse's Update Manager: http://www.scala-lang.org/scala-eclipse-plugin.

Unzip the Scala download package, such as C:\ Program Files\ Java\ scala-2.7.5, then set the environment variable, set SCALA_HOME to: C:\ Program Files\ Java\ scala-2.7.5, add SCALA_HOME\ bin to the Path environment variable, open the command line and type: scalac-version test. If you can enter the version information correctly, you will OK.

Get to know Scala.

In the field of computer science, everyone has reached a tacit understanding that "HelloWorld" is usually used as a demonstration program, and there is no exception here. The syntax of Scala is still very different from that of Java, just like a foreign Daniel criticized that Scala is "different for different reasons". Why can't you use the syntax similar to Java like Groovy? if you transfer from JDK to Scala, the feeling of learning Scala may be very different. There may also be feelings of rejection, in fact, usually this feeling may be due to unfamiliarity with it, and a deeper understanding of the design intentions behind it may change your current point of view.

Let's take a look at the HelloWorld writing in Java:

Public class HelloWorld {public static void main (String [] args) {System.out.println ("HelloWorld");}}

HelloWorld of Scala:

Object HelloWorld {def main (args: Array [String]) {System.out.println ("HelloWorld");}}

The way Scala declares the class here is different from that of Java. Scala is defined by object, starting with the definition of the function "def" in the method block, then declaring the function name, specifying the parameter list in parentheses, and declaring the parameter name as the parameter name: the type of the parameter, and the return value type of the method is specified after the parenthesis after the function name, for example, you declare a method A with no parameters. The return value type is Int, which can be written as follows: def A (): Int = {/ / method content} The specified return value shown in the main function above is Unit by default, which is equivalent to Void in Java. However, the return type of Unit has been set to @ Deprecated in the version of * *, that is, the specified return type Unit that does not need to be displayed is much simpler.

At this point, I believe you have a deeper understanding of "Scala how to write Hello World". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report