Liaoliang "DT Big Data Dream Factory" second talk about Scala function definition, Process Control, exception handling get started
Do you want to know big data, do you want to be a yearly salary million? So what are you waiting for, come on! Follow Liaoliang's teacher to learn spark big data
The second lecture is about Scala's function definition, Process control, and exception handling.
function definition:
Keyword (def) function name parameter (parameter name: parameter type): return content type = {
function body
}
Attention:
Unit: Empty return content
Scala closing is not required to write a semicolon
The following code is an example:
With no parameters
Object scalabasics{
Def doWhile () {
var line = ""
do{
line = ReadLine ()
Printin ("Read:" + line)
}while (line! = "")
}
def main (Args:array [String]) {
Dowhile
}
Note: Dowhile after the () can be added, no parameters are not written
With parameters
def looper (X:long, y:long): Long = {
var a = X
var b =y
while (A! = 0) {
Val temp = a
A = b% A
b = Temp
}
B
}
def main (Args:array [String]) {
Printin (Looper (100,298))
}
Process Control
def main (Args:array [String]) {
var file = "Scala.txt"
if (!args.isempty) file = args (0)
}
Printin (file)
var file = if (!args.isempty) args (0) Else file = "Scala.xml"
Printin (if (!args.isempty) args (0) Else file = "Scala.xml")
For (I <-1 to 10) {
Printin ("number is:" +i)
}
1to10 Another way of writing
For (I <-1.to (10))
val files = (new Java.io.file (".")) Listfiles ()
For (file <-files) {
Printin (file)
}
Object scalabasics{
var line = ""
do{
Printin ("Please input some words blow ...")
line = ReadLine ()
Printin ("Read:" + line)
}while (line! = "")
}
Exception handling
Val N =99
Val file = "Spark.txt"
OpenFile (file)
try{
Val half = if (n% 2 ==0) N/2 else throw
New RuntimeException ("N must be Event")
Use the file
}catch {
Case e:exception = Printin ("The Exception is:" + e.getmessage ())
}finally{
Close (file)
}
The basic understanding of Scala's features and function definitions, process control, exception handling, and mastery, is based on a simple code example, hoping to follow the teacher's 1.1-point progress.
If you know it's unclear, you can also learn this video or record the contact information
Video Address: http://www.tudou.com/programs/view/eN90M1SI8E8/
Study Address: http://mp.weixin.qq.com/s?__biz=MzAwNjAwODI3Mg==&mid=212579488&idx=2&sn= 883193df2b3df163d49c4fdec1ecd585&scene=5#rd
Liaoliang "DT Big Data Dream Factory" second talk about Scala function definition, Process Control, exception handling get started