Foreword
Today, we continue to learn Scala Programming, the Scala is hard for Beginner, it has functional coding style.
Text
command style is like Java,C++,Python, function style is like Erlang, Scheme, Haskell. About Scala Coding style, it has two styles: function coding style and command coding style. I recommand to use function coding style
, the reason is function coding could reduce the program when it produce bugs. And the style that we could reduce code quantities. Through the below Codes, we together look two different styles.
object day4 {def main(args:Array[String]): Unit = {System.out.println("Scala is make me happy!!");val STRING_array = Array("Scala","Java","C++","Julia","Python","R","C") ;System.out.println("The printArgs:");printArgs(STRING_array);System.out.println("The printArgs2:");printArgs2(STRING_array);System.out.println("The printArgs3:");printArgs3(STRING_array)}def printArgs(args:Array[String]): Unit = {var n = 0;while (n < args.length){println(args(n));n += 1;}}def printArgs2(args:Array[String]): Unit = {for (arg <- args){println(arg);}}def printArgs3(args:Array[String]): Unit = {args.foreach(println)}}
Output Result:
From the above result, we know the program have var type defined
,
haven’t use var type and use for loop, haven’t use for loop and var type declared , we could know the codes are mostly reduce, and the program become easily protect and repair. The key place is val type and var type, val type
is function coding style, var type
is command coding style. Val is immutable type, it could make less mistakes【Bugs】. And it could improve calculation performance when data is biggest.
However, if you need use command coding style to finish tasks conveniently, No problem, I advise you use the Command coding style. The Scala is good in the compatibility, it could achieve your different requestions.
In the end
I gratitude for your watch my articles, if you seek some wrongs, please you point them, I thanks for you. Please you insist to learn technologies, maybe it’s hard, but I believe in you will get a good result. Cheers!! my dear friends, your Pan【One human of low-quality men】