Scala-02 function definition, Process control, exception handling

Source: Internet
Author: User

scala-02 function definition, Process control, exception handling

First, theScala function definition:

( The package is imported by default when the Scala function is defined)

(1)val: variable (constant) similar to final type in Java

1, basic format: val variable name: variable type = value

2, where variable types can be omitted and can be automatically identified

3, variable cannot be assigned, modify

var: equivalent to a non- final type variable in Java

(2) function definition with parameters

def function name (parameter list,...) : Returns the result type ={}

Object myscala{

def mysca (X:int, y:int): int={

Var a=x

Var b=y

while (a!=0) {

Var Temp=a

B=b%a

B=temp

}

B

}

Println (Mysca (120,230))

}

① Sometimes a function must have a return result type, such as a function recursive, it must explicitly formulate the return result type;

② If the function has only one statement, the curly braces may not be written. As in the case of the max function , the function is still unchanged;

③ The Scala language does not add () when making function calls;

④ The Scala language can also be written without adding ";"

⑤ Scala ,i++ or ++i do not work, they can only use i=i+1

⑥ foreach and function literals

function literal format: parameter list = function Body

(X:int, Y:int) = x + y

⑦ The For Loop is a val type, so It cannot be re-assigned in the for Expression , <- can be understood as meaning for (Arg <-args)

Note: Scala Ternary symbol expression example:

var file=if(!args.isempty)args (0) Else Scala.xml

Second, Exception Handling

The exception mechanism in Scala is the same as in Java and C + + . However , there is no exception to be examined in Scala, that is, there is no need to explain what exception the function method might throw.

Use throw to throw an exception. The type of the throw expression is nothing.

If you do not need to use the caught exception object, use _ instead of the variable name (_ is a wildcard character in Scala) and there are try/ Finally statement, usually used to clean up. Can be combined into try/catch/finally Statements

Throwing an exception looks similar to Java. Create an exception object, and then use the throw keyword to throw it

throw new IllegalArgumentException

Catch exception:ry/catch catches any exceptions in a separate block, and then uses case blocks for pattern matching.


This article is from the Big Data technology-scala blog, so be sure to keep this source http://10596633.blog.51cto.com/10586633/1682039

Scala-02 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.