Scala is a statically typed programming language for the Java Virtual Machine that aims to be concise, object-oriented, and functional. Some key features of Scala include type inference, pattern matching, actors for concurrency, and compiling to Java bytecode. Scala improves on Java by handling null values safely by default and providing more concise syntax for working with collections, XML, and other common tasks. Scala is growing in popularity as a potential successor to Java due to its interoperability while improving on Java in several ways.
8. Scala Type Inference Uniform Access Principle Closures Higher Order Functions Pattern Matching Actors Generics Native XML support Abstract control structures Implicit conversions and parameters Advanced for expressions Annotations Combinator parsing Traits Duck typing Null „safety”
9. „If I where to choose language other than Java it would be Scala”
10. „I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy”
11. „Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable”
12.
13. Scala - Basics class Test { // variable definition var n = 5 // value definition val i = 10 // function definition def printit(x: Any) = println(x) def sum(i: Int, n: Int) = i+n } object Test extends Test { def main(args: Array[String]): Unit = { printit(sum(i,n)) } }
19. Null’s - Scala val capitals = Map("Poland" -> "Warsaw"); val capitalOption: Option[String] = capitals.get("Polska") capitalOption match { case Some(value) => println(value) case None => println("Not found") case _ => } if(capitalOption.isDefined) println(capitalOption.get) println(capitalOption getOrElse "Not found")
20. XML val xml = <a href="http://example.com"> <customTagb="foo"> <deepTagb="bar" /> Custom content </customTag> </a> println(xml "customTag") println(xml "deepTag") println(xml "@b") println(xml "customTag" "deepTag" "@b") Outputs: <customTag b="foo"><deepTag b="bar"></deepTag>Custom content</customTag> <deepTag b="bar"></deepTag> foobar bar
21. XML val xml =<divclass="rsswidget"> <ul> <liclass="rsswidgettitle"><ahref="http://www.quotationspage.com/qotd.html">Quotes of the Day</a></li> <liclass="rsswidgetitem"><ahref="http://www.quotationspage.com/quotes/C._P._Snow">C. P. Snow</a></li> <liclass="rsswidgetitem"><ahref="http://www.quotationspage.com/quotes/unknown">unknown</a></li> <liclass="rsswidgetitem"><ahref="http://www.quotationspage.com/quotes/Frederick">Frederick</a></li> </ul> ... </div> (xml "@href").slice(1,4).foreach(url => println("URL:"+url) val con = new URL(url.toString).openConnection val reader = newBufferedReader(newInputStreamReader(con.getInputStream, "ISO-8859-1")) var line = ""; while(line != null) { line = reader.readLine if(line != null) println(line) } con.getInputStream.close )
22. Actors Easier concurrency Message passing instead of locks Asynchronus Implemented using Java threads ScalaActors, Akka, Lift - CometActor
23. Actors class ActorUser { def send(msg: Any) = ScalaActor ! msg } object ScalaActor extends Actor { /* The $actor's mailbox. */ // private[actors] val mailbox = new MQueue[Msg]("Reactor") def act() { receive { case"ping!" => println("pinged!"); case ("ping!", actor: Actor) => actor ! "pong!" case msg => println("unhandled message type:"+msg) } } }