scala学习笔记☞二:简单语法练习
2009-02-17 14:29
441 查看
1. var 和 val的区别
A val is similar to a final variable in Java. so, once initialized ,that can never be reasigned.
A var is similar to a no-final variable in Java.
2. define some functions;
def max(x:Int,y:Int):Int={
if(x>y)
x
else
y
}
def: starts a function definition
max: function name
x:Int,y:Int : parameter list in parentheses
:Int : function's result type
Note: 1) scala's if expression can result in a value, So, It's similar to java's ternary operator(x?y:z).
2) sometimes the scala compiler will require you to specify the result type of a function. If the function don't has a result,but you to specify unit tha is the result type . But, If the function is recursive ,for example , you must explicitly specify the function's result type.
3. Array of the scala
In scala,arrays are zero based,as in java, buy you access an element by specifying an index in parentheses rather than square brackets.
eg: the first element in a scala array named steps in steps(0),not steps[0].
4. comment of the scala
as with Java.
5. Loop with While and decide with if
1> Java's i++ and ++i don't work in scala! To increment in scala ,you need to say either i=i+1 or i += 1;
2> You write while loops in scala in much the same way as in java.
6. Iterate with foreatch and for
eg:
def main(args:Array[String])={
var arg = Array("hello","scala","great");
println(" 1:")
arg.foreach((arg:String)=>print(arg+" "))
println
println(" 2:")
//要注明类型时需要括号,否则括号可省略。
arg.foreach(arg=>print(arg+" "))
println
println(" 3:")
//if a function literal consists of one statement that takes a single statement,
//you need not explicitly name and specify the argument.
arg.foreach(println)
println("-----");
for(argItem <- arg){//Note: argItem is a val.(not var)
print(argItem+" ")
}
}
A val is similar to a final variable in Java. so, once initialized ,that can never be reasigned.
A var is similar to a no-final variable in Java.
2. define some functions;
def max(x:Int,y:Int):Int={
if(x>y)
x
else
y
}
def: starts a function definition
max: function name
x:Int,y:Int : parameter list in parentheses
:Int : function's result type
Note: 1) scala's if expression can result in a value, So, It's similar to java's ternary operator(x?y:z).
2) sometimes the scala compiler will require you to specify the result type of a function. If the function don't has a result,but you to specify unit tha is the result type . But, If the function is recursive ,for example , you must explicitly specify the function's result type.
3. Array of the scala
In scala,arrays are zero based,as in java, buy you access an element by specifying an index in parentheses rather than square brackets.
eg: the first element in a scala array named steps in steps(0),not steps[0].
4. comment of the scala
as with Java.
5. Loop with While and decide with if
1> Java's i++ and ++i don't work in scala! To increment in scala ,you need to say either i=i+1 or i += 1;
2> You write while loops in scala in much the same way as in java.
6. Iterate with foreatch and for
eg:
def main(args:Array[String])={
var arg = Array("hello","scala","great");
println(" 1:")
arg.foreach((arg:String)=>print(arg+" "))
println
println(" 2:")
//要注明类型时需要括号,否则括号可省略。
arg.foreach(arg=>print(arg+" "))
println
println(" 3:")
//if a function literal consists of one statement that takes a single statement,
//you need not explicitly name and specify the argument.
arg.foreach(println)
println("-----");
for(argItem <- arg){//Note: argItem is a val.(not var)
print(argItem+" ")
}
}
相关文章推荐
- Scala学习笔记-环境搭建以及简单语法
- 黑马程序员之JS学习笔记:简单的LINQ的查询语法和方法语法
- Scala学习笔记(三)Actor简单例子
- 黑马程序员--学习笔记--一个WinForm简单加法练习程序
- Scala学习笔记:重要语法特性
- Scala学习教程笔记一之基础语法,条件控制,循环控制,函数,数组,集合
- C语言学习笔记(九)---简单的RSA加密算法练习
- scala学习笔记5 spark-shell的简单操作
- Spark学习笔记1-Scala基础语法
- Ruby学习笔记:语法编程练习
- scala学习笔记(1):基本语法与容器
- Scala 学习笔记(4)-集合类简单操作
- Scala学习笔记:数组操作与简单算法示例
- iOS学习笔记04——Visual Format Language语法的简单学习
- [每日学习笔记][2013.03.24]MYSQL的语法和简单介绍(二)
- Scala基础语法学习笔记
- scala学习笔记☞三:语法续①
- oracle 最简单的学习笔记,增删改查,PLSQL基本语法,游标,函数,存储过程的实现
- 【Matlab学习笔记】(四)认识matlab的文件类型和简单练习
- Scala基本语法学习笔记