Scala type declaration and definition, function definition, Process control, exception handling

Source: Internet
Author: User
Tags case statement

The Scala source code is compiled into Java bytecode, so it can run on top of the JVM and can invoke an existing Java class library.the basic types of Scala are basically consistent with JavaScript.

Scala's data types are all the same in Java, with the same memory footprint and precision. The following table is about the details of all the data types available in Scala:

Data Type Description
Byte A 8-bit signed value. range from 128 to 127
Short A 16-bit signed value. range from 32768 to 32767
Int A 32-bit signed value. range from -2147483648 to 2147483647
Long A 64-bit signed value. From 9223372036854775808 to 9223372036854775807
Float 32-bit IEEE754 single-precision floating-point number
Double 64-bit IEEE754 double-precision floating-point number
Char 16-bit unsigned Unicode characters. range from u+0000 to U+FFFF
String Character sequences
Boolean Either literal true or false literal
Unit Corresponds to No value
Null Null or empty reference
Nothing Each of the other types of sub-types; Include no value
Any Any type of super type; Any object is of any type
Anyref Super type of any reference type
Variable declaration

Scala's variable declarations have different syntaxes, which can be defined as values, that is, constants or variables. The following is the syntax for defining a variable using the var keyword:

var myvar:string= "Foo"

Here, MyVar uses the keyword VAR declaration. This means that it is a variable that can change the value, called a mutable variable. The following is the syntax for defining variables using the VAL keyword:

Val myval:string= "Foo"

Here, Myval is using the keyword Val declaration. This means that it is not possible to change the variable, which is called an immutable variable.

Variable data type:

The variable type is specified after the variable name and is signed before equals. Any type of scala variable can be defined by its data type as follows:

Val or Val Variablename:datatype[=initialvalue]

If you do not specify any initializer variables, it is valid as follows:

var myvar:int;val myval:string;
Variable type inference:

When assigning an initial value to a variable, the Scala compiler can calculate the variable type based on the value assigned to it. This is known as variable type inference. Therefore, the declaration of these variables can be written as follows:

var myVar =10;val myval = "Hello, scala!";

In this case, by default, MyVar is of type int

The method is defined as follows:function definition and python a little bit of a coincidence, the only wonderful thing, the name of the parameter before the type, plus: separate. The return value follows the function parameter. a little bit special.  def f (x:int) = {x*x}def f (x:any): The parentheses can be omitted when Unit = println (x) function calls without arguments. Names foreach (n = println (n)) names mkstring "," Optstr getorelse "<empty>"def Add (i:int,j:int): int{return i+j}variable length parameterdef add (x:int*) = {For (i <-x) {println (i)    }X.sum}def main (args:array[string]): Unit{Add (10,10,10)Add (1 to: _*)} how Scala defines static methods: Scala does not have a static method by default, and if you need a static method, you can define the method in object by defining the object class (see later article). Scala, like C + +, has default parameters, and Python types, and you can specify parameters to assign values. The only difference is his variable length parameter.  
 For (i <-1 to ten if I% 2 = = 0) {println (i)}for (i <-0 until Array.lenth) {println (i)}

For(i <-0 to reverse) print (i)

val foo = for (x <-0 until (ten, 2)) yield x for (i <-array_name) {//todo}scala> 1.to (Ten) Res0:scala.collect Ion.immutable.Range.Inclusive = Range (1, 2, 3, 4, 5, 6, 7, 8, 9, ten) scala> 1.until (Ten) res1:scala.collection.immutable. Range = Range (1, 2, 3, 4, 5, 6, 7, 8, 9)The same is true of exception handling.
Scala's pattern matching not only matches the pattern, but also the case class/object (most important), as well as the type match. (Up/down type) conversions can be matched.

The match statement in Scala is used to select a branch in a list to execute a branch of the statement block, similar to Swtich in other languages. Case statement

Here's a simple example.

var MyVar = "Thevalue"; MyVar match {case "somevalue" = println (MyVar + "1");   Case "Thisvalue" = println (MyVar + "2");   Case "Thevalue" = println (MyVar + "3"); Case "Doublevalue" = println (MyVar + "4");}
sometimes cases are caught by default.the first value matchesThe second type of configuration third, array,list and other containers match fourth, case Calss style class

Scala type declaration and definition, function definition, Process control, exception handling

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.