Scala applet details and example code

  • 2020-06-01 09:50:29
  • OfStack

Scala small program details

The & # 65279; 1. Interactive mode

In the command line window, enter the Scala command:


xiaosi@Qunar:~$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.
scala> 

The first small program:


xiaosi@Qunar:~$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.
scala> println("Hello world")
Hello world
scala> 

2. Script mode

Script mode 1 of the small program:


object Test{
 def main(args: Array[String]){
  println("Hello " + args(0) + " ....")
 }
}

Let's take a look at how to save the file, compile, and run the program. Follow these steps:

(1) save the code as Test.scala

(2) open the command window, then go to the directory where the program files are saved, which is /home/xiaosi/test

(3) compilation


xiaosi@Qunar:~/test$ scalac test.scala 

The above command will generate several class files in the current directory:


xiaosi@Qunar:~/test$ ls
sh_env.sh sh_env.sh~ Test.class Test$.class test.scala tomcat-bin-sh

One of them is called Test.class, which is a bytecode that can run on the Java virtual machine (JVM)

(4) operation


xiaosi@Qunar:~/test$ scala test.scala apple
Hello apple ....
xiaosi@Qunar:~/test$ 

You can see Hello apple... The output

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: