Scala Self-Study Diary (2)-use Scala interpreter (interpreter)

Source: Internet
Author: User

In the previous article we described how to install Scala and how to configure environment variables

Next we'll explain how to use the interpreter to write simple Scala code

Open the console and enter Scala development mode:

bokee@debian70:~$ Scala
Welcome to Scala version 2.9.2 (Java HotSpot (TM) 64-bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type:help for more information.




1. Simple output

scala> 1+2
res0:int = 3


Direct Input 1 + 2, return res0:int = 3

RES0 represents the variable name, int represents the type, and 3 is the value.

Scala's type definition is followed by a colon, unlike Java, which is defined in the same way as a UML class diagram.

Scala is a strongly typed language and you have to define the type, but the Scala compiler is smart enough to help you define the type of the variable based on your values.

Here Res0 is a variable name, so you can use it next.

2. Simple test again

scala> res0*3
res1:int = 9

The interpreter also gave a variable res1.
All Scala's variables are objects, and all operations are methods.
So * is also the way you can call

Scala> res0.* (res1)
res2:int = 27
3. Simple output information

Scala> println ("Test msg!")
Test msg!

This println is a method, because Scala has predefined imports of some classes, so it can be used directly.

4. Define Variables

Scala has two types of variables, Val and var.

The value of the Val variable can only be initialized once, and an error occurs when the value is assigned again, and Var is the same as the Java variable, and can be modified at any time.
Val is the style of functional programming, and when a variable is assigned, don't make any changes, there will be a lot of benefits from the program, but sometimes it can be done in a more round way.

Val

Scala> val msg:string= "Hello world!"
msg:string = Hello world!

scala> msg= "Sssss"
<console>:8:error:reassignment to Val
       msg= "Sssss"
          ^


Var:

scala> var msg2= "Hello world!2"
msg2:java.lang.String = Hello world!2 scala> msg2=

"Ssssss"
MSG2: java.lang.String = ssssss

scala> println (msg+ ";" +MSG2)
Hello world!; Ssssss



5. Definition Method:

Scala> def Max (X:int, y:int): int = if (x < y) y else x
max: (X:int, y:int) int

scala> max (1,2)
Res 2:int = 2

Define variables with Val and Var, define methods with def.
The Max method has a method parameter, a return value type, a method body. Though small, spite.

Scala's variable representation is variable name: type, which is very different from Java and even other languages, and is closer to the UML style.


The Scala method is also a type, or a value, that becomes the class one.
So you can think of Max as a variable name, (int,int) Int is its type. He can pass as a parameter or assign a value to another variable.

Scala> Val M=max _
m: (int, int) => int = <function2>

scala> m (1,3)
Res3:int = 3

Note that the "_" inside can not be omitted, and Max and "_" between the space

OK, this is the end of this 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.