Because the scala jvm is running on, so can run java, in principle, be able to run scala
1, international conventions, first to a hello world walk
Package the demo1 // under full static object, this stuff Scala no static object the HelloWorld { // DEF behind the args argument function definition is followed by a colon and then the parameter type to the opposite of the java, if java, so that args String [] / / after the last main () then: is then feedback function representing no feedback type Unit type DEF main (args: the Array [String]): Unit = { // Once inside, like how to how. . . . // There scala can not write a semicolon Oh, really for the national provincial cloth. . . . the println ( "Hello World" ) } }
2. Well, then we simply write a few programs, Basics identifies it scala
File Operations
Package the demo1 Import java.io.File Import scala.io.Source Object FileDemo2 { DEF widthOfLength (S: String) = s.length.toString.length DEF fileLines (File: File) = Source.fromFile (File) .getLines () .ToList // static variable Val filesHere = new new file ( "./ the src / the demo1" ) .listFiles () DEF grep (pattern: String) = // here with () or {} are for { // iterate through all files File <- filesHere IF file.getName.endsWith ( "Scala." ); //遍历这个文件的所有行 line <- fileLines(file) if line.trim.matches(pattern) } { println(file + " : " + line.trim) } def main(args: Array[String]): Unit = { if(args.length > 0) { val lines = Source.fromFile(args(0)).getLines().toList val longestLine = lines.reduceLeft((a, b) => if (a.length > b.length) a else b) val maxWidth = widthOfLength(longestLine) for(line <- lines) { val numSpace = maxWidth - widthOfLength(line) val padding = " " * numSpace println(padding + line.length + " | " + line) } } else { Console.err.println("Please enter filename") } grep(".*main.*") } }
Using java to achieve this function, not a few hundred lines of estimates can not handle. . .
Just file input and output io operation code, what File = new File What InputStream, what a bunch of ReaderBuffer estimate will write a dozen lines
Here then compare scala surprise is that circulation, especially for cycling, it is really more powerful
3. Finally, we look at the application for the recycling of scala why so powerful it, especially the yield of use, it is really convenient, direct us to avoid the re-java for loop or while loop, and so the object needs to be saved when the list.add operation here directly ready to throw you, do you want to see
If you want to add yield, do not add, it is convenient
package demo2 class FileDemo2 { def makeRowSeq(row : Int) = for(col <- 1 to 10) yield { val prod = (row * col).toString val padding = " " * (4 - prod.length) padding + prod } def makeRow(row : Int) = makeRowSeq(row).mkString def multiTable() = { val tableSeq = for(row <- 1 to 10) yield { makeRow(row) } tableSeq.mkString("\n") } } object FileDemo2 extends App { implicit def add(x : Int) = 2 * 2 for(season <- List("fall", "winter", "spring")) { println(season + ": ") } var a = 'asdasda val a1 = "asdasda" println(a + " ,nameis:" + a.name + s", value is:$a1 -> $a") println(2) val fileDemo2 = new FileDemo2 val listDemo2 = fileDemo2.multiTable() println(listDemo2) }
好啦,今天的学习就到这了,目前来看scala很像java的简化版本,毕竟是后java出来的,有些简化很正常,目前还没发现scala的独到的地方
当然除了它写代码确实很简洁,java几百行,这个用scala可能就十几行,或者几十行,代码量至少缩小一倍吧
后续学习,我们接着来挖掘scala吧