Scala basic syntax

Source: Internet
Author: User

declaring variables

Val means that the constant VAR represents the variable regardless of who must initialize the write variable when the name is in front of the type after the input Output statement print, println, printfreadline Common type Byte, Char, Short, Int, Long, Float,  Double, Boolean no lowercase, only uppercase no base type, all class their parent class is any, empty type uses () that is the unit operator using the method operator, essentially the method 1+2 is actually 1.+ (2) 1 to 10 is actually 1.to (10) The return value statement does not need to write return by default, but the last line is returned by default, but the assignment statement does not return a value with a unit representation of the conditional expression if (1>0) 1 else 0if (1>0) 1 else () for loop for (I <-1 to Ten) println (i) for (I <-1 until) println (i) for (I <-Array ("Hello", "You")) println (i) Multiple for (i<-1 to 3;j<-1 to 3) println (I*10+J) deduction type for (I <-1 to) yield i*2 Guardian for (I <-1 to ten if i%2==0) yield i*2  function def func (...) = ... A recursive function must have a return value type when called, and if no argument is available, you can not write the parentheses parameter has a default value a known parameter is a variable-length array Process def func (...) ... Indicates that no return value array array is fixed-length, definition of the same type: New Array[int] (2) and array (on) access arr (0) Assignment arr (0) =100 algorithm: Arr.sum output: Arr.mkstring ("and")
Variable group Import Scala.collection.mutable.ArrayBuffer

Arraybuffer[int] () or arraybuffer[int]

ab+=1ab+= (1,1,1) Ab++=array (1,1,1) Ab.insert (2,6) ab.remove (index,length) Ab.toarray map the dual type in the map is the same map ("K1"->1, "K2"->2) Map (("K1", 1), ("K2", 2)) M.getorelse ("B", 0) m ("a") =10m + = ("K3"->3) m-= "K3" for ((k,v) <-m) ... Import Scala.collection.mutable.Map the element type in the tuple can be different (1, "A") to value a._1 zipper "Hello". zip ("World") class-person {var-age = 0}

Generate private field age, Read Method age (), and Write Method age_= (...)

Can implement only Read method No write method cannot implement only write method not read method If you need to call each other with Java, you must generate a real getter and setter method

CALSS person{@BeanProperty var age=0}

The class's main constructor is intertwined with the class definition,

Class Person (var name:string = "A", var age:int = 1) {

println ("I was created")

def desc () = name+ "," +age

println (THIS.DESC)

}

The statements in the main constructor will not be instantiated when instantiated, unless the name of the secondary constructor auxiliary constructor with the default value class is this

Class Person {

var age=0

def this (age:int) {

This ()//Call main constructor

This.age = Age

}

}

Call new Person (12) or new person

Scala basic syntax

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.