Слайды и текст доклада
Pic.2
Types Primitives char byte short int long float double boolean Objects String … and many more …
Pic.3
Type declarations int x; final int Y = 0; int[] langs = {"C++", "Java", "Scala"}; Set<String> langs = new Set<>(); langs. add("C++"); langs. …
Pic.4
“Statements” Scala’s “statements” should really be called “expressions,” because every statement has a value The value of many statements, for example the while loop, is () () is a value of type Unit …
Pic.5
Constructors class Point { private double x; private double y; public Point(double x, double y) { this. x = x; this. y = y; } . . . }
Pic.6
Getters Java: Make the instance variables private, and write getter methods class Point { private double x, y; public double getX() { return x; } } Point p = new Point(3. 6, 4. 7); System. out. …
Pic.7
Both getters and setters Java: Write as methods public double getX() { return x; } public void setX(Double x) { this. x = x; } p. setX(2 * p. getX());
Pic.8
Auxiliary constructors Java: Can have multiple constructors, which may or may not refer to one another public Point(double x, double y) {. . . } public Point() { this(0, 0); }
Pic.9
Defining equality for objects @Override public boolean equals(Object other) { if (other instanceof Point) { Point that = (Point) other; return this. x == that. x && this. y == that. y; } …
Pic.10
Case classes in Scala A case class is just like a regular class, except: The methods equals, hashCode, toString, and copy are automatically defined for you, based on the parameters to the primary …
Pic.11
Input and output Scanner scanner = new Scanner(System. in); System. out. println("What is your name? "); String name = scanner. nextLine(); System. out. println("Hello, " + name);
Pic.12
Singleton objects class Earth { final double diameter = 7926. 3352; Earth earth = e; private Earth() {} public instanceOf() { if (e == null) e = new Earth(); return e; } }
Pic.13
Operators in Scala Scala has the same arithmetic and logical operators as Java, except: ++ and -- have been removed test ? iftrue : iffalse has been replaced by if (test) iftrue else iffalse which is …
Pic.14
Familiar statement types These are the same as in Java, but have a value of ( ): variable = expression // also +=, *=, etc. while (condition) { statements } do { statements } while (condition) These …
Pic.15
The for comprehension Scala’s for is much more powerful than Java’s for Consequently, it is used much more often than the other kinds of loops We will just cover some simple cases here for (i <- 1 …
Pic.16
for…yield for returns Unit, but for…yield returns a sequence of values Where possible, it returns the same type of sequence as it operates on scala> for (i <- List(1, 2, 3)) yield { 2 * i } …
Pic.17
Explicit pattern matching Explicit pattern matching is done with the match method: expression match { case pattern1 => expressions … case patternN => expressions }
Pic.18
Pattern matching Pattern matching on literal values: today match { case "Saturday" => println("Party! Party! Party!") case "Sunday" => println("Pray. . . . …
Pic.19
The Option type Scala has null because it interoperates with Java; it shouldn’t be used any other time Instead, use an Option type, with values Some(value) and None def max(list: List[Int]) = { if …
Pic.20
Java 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 …
Pic.21
Pet peeves Here are some things that annoy me about Java but are fixed in Scala == works for strings about 95% of the time If you write a constructor for your class, the default constructor vanishes …
Pic.22
Scala is like Java, except when it isn’t 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 …
Pic.23
Consistency is good 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 …
Pic.24
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 …
Pic.25
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. …
Pic.26
null in Scala In Java, any method that is supposed to return an object could return null Here are your options: Always check for null Always put your method calls inside a try. . . catch Make sure …
Pic.27
Uniform access In Java, myString. length() is a function, but myArray. length is a variable If age is a public field of Person, you can say: david. age = david. age + 1; but if age is accessed via …
Pic.28
Concurrency “Concurrency is the new black. ” Broadly speaking, concurrency can be either: Fine-grained: Frequent interactions between threads working closely together (extremely challenging to get …
Pic.29
Scala is multiparadigm Scala is an attempt to blend object-oriented programming with functional programming Here’s the difficulty: Objects have state—that’s practically their only reason for being …
Pic.30
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 …
Pic.31
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 …
Pic.32
“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 …
Скачать презентацию
Если вам понравился сайт и размещенные на нем материалы, пожалуйста, не забывайте поделиться этой страничкой в социальных сетях и с друзьями! Спасибо!