際際滷

際際滷Share a Scribd company logo
A Brief Introduction to Scala
@pranavek
 Scala is new generation JVM language
 It is intended to be compiled to Java bytecode
 The resulting executable runs on the JVM
 The name Scala is a portmanteau of "scalable" and
"language" (SCAlable LAnguage)
 James Strachan, the creator of Groovy, described
Scala as a possible successor to Java
Scala Vs Java
 Similar to Java compiler javac, Scala has a compiler
scalac, which compiles Scala code into byte code
 You can call Scala from Java and Java from Scala, it
offers seems less integration
 Major Java programming IDE like Eclipse, Netbeans
and InetelliJ supports Scala
 Both are OOP, Scala goes one steps further and also
supports functional programming paradigm
 Type inference
 Lazy evaluation
 lazy val images = getImages()
if(viewProfile){
showImages(images)
}
else if(editProfile){
showImages(images)
showEditor()
}
else{
//do something without loading images
}
 Scala treats any method as they are variables
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
object declaration (Singleton object: class with single object)
Declares both a class called HelloWorld and an instance of that
class, also called HelloWorld
The classic Hello, World! program
 Convenience and flexibility
var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo")
println(capital("France"))
 The notation in this example is high-level, to
the point
 Not cluttered with extraneous semicolons or type
annotations.
 Scala gives you fine-grained control if you need it
val x = new HashMap[Int, String]((1,Hello))
val x: Map[Int, String] = new HashMap()
 Growing new types
- Eric Raymond - cathedral and bazaar as two
metaphors of software development
 scala.BigInt
def factorial(x: BigInt): BigInt =
if (x == 0) 1 else x * factorial(x - 1)
 Now, if you call factorial(30) you would get:
265252859812191058636308480000000
 java.math.BigInteger
import java.math.BigInteger
def factorial(x: BigInteger): BigInteger =
if (x == BigInteger.ZERO)
BigInteger.ONE
else
x.multiply(factorial(x.subtract(BigInteger.ONE)))
- Attempted to implement all of these abstractions at the
same time would simply become too big to be manageable
- Scala allows users to grow and adapt the language in the
directions they need by defining easy-to-use libraries that
feel like native language support
 Scala is compatible
 It allows you to add value to existing code
 Build on what you already have - because it was designed for
seamless interoperability with Java
 Run-time performance is usually on par with Java programs
 Can call Java methods, access Java fields, inherit from Java
classes, and implement Java interfaces.
 Re-uses many of the standard Java library types.
 str.toInt instead of Integer.parseInt(str)
Why Scala?
 Scala is concise
 // this is Java
class MyClass {
private int index;
private String name;
public MyClass(int index, String name) {
this.index = index;
this.name = name;
}
}
 //scala
class MyClass(index: Int, name: String)
 Scala is high-level
 // this is Java
boolean nameHasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
nameHasUpperCase = true;
break;
}
}
 //scala
val nameHasUpperCase = name.exists(_.isUpperCase)
 Interaction with Java
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object FrenchDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
}
import java.util.{Date, Locale}
import java.text.DateFormat._
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
 A Scala Tutorial for Java programmers [Michel
Schinz, Philipp Haller]
 http://javarevisited.blogspot.in/
 Programming in Scala [Martin Odersky, Lex Spoon,
Bill Venners]
 wikipedia.com
References

More Related Content

scala

  • 1. A Brief Introduction to Scala @pranavek
  • 2. Scala is new generation JVM language It is intended to be compiled to Java bytecode The resulting executable runs on the JVM The name Scala is a portmanteau of "scalable" and "language" (SCAlable LAnguage) James Strachan, the creator of Groovy, described Scala as a possible successor to Java
  • 3. Scala Vs Java Similar to Java compiler javac, Scala has a compiler scalac, which compiles Scala code into byte code You can call Scala from Java and Java from Scala, it offers seems less integration Major Java programming IDE like Eclipse, Netbeans and InetelliJ supports Scala Both are OOP, Scala goes one steps further and also supports functional programming paradigm
  • 4. Type inference Lazy evaluation lazy val images = getImages() if(viewProfile){ showImages(images) } else if(editProfile){ showImages(images) showEditor() } else{ //do something without loading images } Scala treats any method as they are variables
  • 5. object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } object declaration (Singleton object: class with single object) Declares both a class called HelloWorld and an instance of that class, also called HelloWorld The classic Hello, World! program
  • 6. Convenience and flexibility var capital = Map("US" -> "Washington", "France" -> "Paris") capital += ("Japan" -> "Tokyo") println(capital("France")) The notation in this example is high-level, to the point Not cluttered with extraneous semicolons or type annotations. Scala gives you fine-grained control if you need it val x = new HashMap[Int, String]((1,Hello)) val x: Map[Int, String] = new HashMap()
  • 7. Growing new types - Eric Raymond - cathedral and bazaar as two metaphors of software development scala.BigInt def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1) Now, if you call factorial(30) you would get: 265252859812191058636308480000000
  • 8. java.math.BigInteger import java.math.BigInteger def factorial(x: BigInteger): BigInteger = if (x == BigInteger.ZERO) BigInteger.ONE else x.multiply(factorial(x.subtract(BigInteger.ONE))) - Attempted to implement all of these abstractions at the same time would simply become too big to be manageable - Scala allows users to grow and adapt the language in the directions they need by defining easy-to-use libraries that feel like native language support
  • 9. Scala is compatible It allows you to add value to existing code Build on what you already have - because it was designed for seamless interoperability with Java Run-time performance is usually on par with Java programs Can call Java methods, access Java fields, inherit from Java classes, and implement Java interfaces. Re-uses many of the standard Java library types. str.toInt instead of Integer.parseInt(str) Why Scala?
  • 10. Scala is concise // this is Java class MyClass { private int index; private String name; public MyClass(int index, String name) { this.index = index; this.name = name; } } //scala class MyClass(index: Int, name: String)
  • 11. Scala is high-level // this is Java boolean nameHasUpperCase = false; for (int i = 0; i < name.length(); ++i) { if (Character.isUpperCase(name.charAt(i))) { nameHasUpperCase = true; break; } } //scala val nameHasUpperCase = name.exists(_.isUpperCase)
  • 12. Interaction with Java import java.util.{Date, Locale} import java.text.DateFormat import java.text.DateFormat._ object FrenchDate { def main(args: Array[String]) { val now = new Date val df = getDateInstance(LONG, Locale.FRANCE) println(df format now) } }
  • 13. import java.util.{Date, Locale} import java.text.DateFormat._ val df = getDateInstance(LONG, Locale.FRANCE) println(df format now)
  • 14. A Scala Tutorial for Java programmers [Michel Schinz, Philipp Haller] http://javarevisited.blogspot.in/ Programming in Scala [Martin Odersky, Lex Spoon, Bill Venners] wikipedia.com References