30-09-2016, 04:04 PM
1457093271-01scalaintro.ppt (Size: 184 KB / Downloads: 78)
What’s wrong with Java?
Not designed for highly concurrent programs
The original Thread model was just wrong (it’s been fixed)
Java 5+ helps by including java.util.concurrent
Verbose
Too much of Thing thing = new Thing();
Too much “boilerplate,” for example, getters and setters
What’s right with Java?
Very popular
Object oriented (mostly), which is important for large projects
Strong typing (more on this later)
The fine large library of classes
The JVM! Platform independent, highly optimized
Java is a good language, and Scala is a lot like it
For each difference, there is a reason--none of the changes are “just to be different”
Scala and Java are (almost) completely interoperable
Call Java from Scala? No problem!
Call Scala from Java? Some restrictions, but mostly OK.
Scala compiles to .class files (a lot of them!), and can be run with either the scala command or the java command
To understand Scala, it helps to understand the reasons for the changes, and what it is Scala is trying to accomplish
In Java, every value is an object--unless it’s a primitive
Numbers and booleans are primitives for reasons of efficiency, so we have to treat them differently (you can’t “talk” to a primitive)
In Scala, all values are objects. Period.
The compiler turns them into primitives, so no efficiency is lost (behind the scenes, there are objects like RichInt)
Java has operators (+, <, ...) and methods, with different syntax
In Scala, operators are just methods, and in many cases you can use either syntax
Type safety is good, verbosity is bad
Java is statically typed--a variable has a type, and can hold only values of that type
You must specify the type of every variable
Type errors are caught by the compiler, not at runtime--this is a big win
However, it leads to a lot of typing (pun intended)
Languages like Ruby and Python don’t make you declare types
Easier (and more fun) to write programs
Less fun to debug, especially if you have even slightly complicated types
Scala is also statically typed, but it uses type inferencing--that is, it figures out the types, so you don’t have to
The good news: Less typing, more fun, type errors caught by the compiler
The bad news: More kinds of error messages to get familiar with
Verbosity
Java:
class Person { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void String getFirstName() { return this.firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void String getLastName() { return this.lastName; } public void setAge(int age) { this.age = age; } public void int getAge() { return this.age; }}
Scala:
class Person(var firstName: String, var lastName: String, var age: Int)
Source: http://blog.objectmentorarticles/2008/08...ala-part-i
Functional languages
The best-known functional languages are ML, OCaml, and Haskell
Functional languages are regarded as:
“Ivory tower languages,” used only by academics (mostly but not entirely true)
Difficult to learn (mostly true)
The solution to all concurrent programming problems everywhere (exaggerated, but not entirely wrong)
Scala is an “impure” functional language--you can program functionally, but it isn’t forced upon you
Scala as a functional language
The hope--my hope, anyway--is that Scala will let people “sneak up” on functional programming (FP), and gradually learn to use it
This is how C++ introduced Object-Oriented programming
Even a little bit of functional programming makes some things a lot easier
Meanwhile, Scala has plenty of other attractions
FP really is a different way of thinking about programming, and not easy to master...
...but...
Most people that master it, never want to go back
You can write a Fortran program
There’s a old saying: “You can write a Fortran program in any language.”
Some people quote this as “You can write a C program...,” but the quote is older than the C language
People still say this, but I discovered recently that what they mean by it has changed (!)
Old meaning: You can bring your old (Fortran) programming habits into the new language, writing exactly the same kind of program you would in Fortran, whether they make sense or not, and just totally ignore the distinctive character of the new language.
New meaning: You can write a crappy program in any language.
Moral: You can “write a Java program in Scala.” That’s okay at first--you have to start out with what you know, which is Java. After that, you have a choice: You can (gradually) learn “the Scala way,” or you can keep writing crappy Scala programs.