6-Feb-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Generic programming in Java
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Debugging Introduction to Computing Science and Programming I.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Road Map Introduction to object oriented programming. Classes
Remedial Tutorials Week 2 How to think about programming.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
1 Advanced Material The following slides contain advanced material and are optional.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Language Issues Misunderstimated? Sublimable? Hopefuller? "I know how hard it is for you to put food on your family.” "I know the human being and fish.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
1-Sep-15 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CIT 590 Intro to Programming First lecture on Java.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
24-Oct-15 Java to Scala. Types Primitives char byte short int long float double boolean Objects String … and many more … Objects Char Byte Short Int Long.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Andreas Enbohm Capgemini Sverige AB Scala or ”The Free Lunch is Over”
We will talking about story of JAVA language. By Kristsada Songpartom.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Cases and Classes and Case Classes And Other Miscellany 16-Dec-15.
Error messages 25-Apr-17.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Builder Pattern. What’s Builder: TO find a solution to the telescoping constructor Problem: Too many parameters Solution: To get an abstract object, a.
Introduction to Exceptions in Java CS201, SW Development Methods.
13-Jun-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Chair of Software Engineering Void safety these slides contain advanced material and are optional.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Type Systems Terms to learn about types: Related concepts: Type
Lesson 2: Building Blocks of Programming
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Class Structure 28-Nov-18.
Generic programming in Java
Scala Apologia 1-Jan-19.
Exceptions 19-Feb-19.
Class Structure 25-Feb-19.
Java Programming Language
CIS 110: Introduction to Computer Programming
Exceptions 10-May-19.
Presentation transcript:

6-Feb-16 Scala Apologia

2 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 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 Statically typed (more on this later) The fine large library of classes The JVM! Platform independent, highly optimized

3 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 (almost) completely interoperable Call Java from Scala? No problem! Call Scala from Java? Some restrictions, but mostly OK. No problem— if you can supply the right type of parameters 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

4 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 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

5 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

6 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:

7 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 the method can’t possibly return null Ignore the problem and depend on luck Yes, Scala has null --but only so that it can talk to Java In Scala, if a method could return “nothing,” write it to return an Option object, which is either Some( theObject ) or None This forces you to use a match statement--but only when one is really needed!

8 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 methods, you would say: david.setAge(david.getAge() + 1); You have to know whether a piece of data is implemented as a variable or as a function In Scala, if age is a public field of Person, you can say: david.age = david.age + 1; but if Person defines methods age and age_=, you would say: david.age = david.age + 1; In other words, if you want to access a piece of data in Scala, you don’t have to know whether it is computed by a method or held in a simple variable This is the principle of uniform access Scala won’t let you use parentheses when you call a function with no parameters

9 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 right) Coarse-grained: Infrequent interactions between largely independent sequential processes (much easier to get right) Java 5 and 6 provide reasonable support for traditional fine-grained concurrency Scala has total access to the Java API Hence, it can do anything Java can do And it can do much more (see next slide) Scala also has Actor s for coarse-grained concurrency

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 For arrays, length is a variable; for Strings, it’s a function ArrayList strings = new ArrayList (); Special syntax for arrays (only), not for any other data structures Adding elements to a list or a map, one at a time Having to explain ++ and -- to new students By default, assert doesn’t do anything Having to write getters and setters to make my code “clean,” even when they aren’t really needed Having to create a Scanner to do simple input (for that matter, all file I/O) Checked exceptions No tuples! NullPointerException s

11 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 Functional programs are stateless Scala tries to bridge this gap Functions in Scala are first-class objects Scala encourages immutable objects All the usual functional programming functions— map, filter, fold, etc.—are available in Scala

12 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

13 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.

15 Genealogy Scala Java C C++ Simula Smalltalk Prolog Erlang Haskell ML Lisp functional programming syntax objects pattern matching Actors Clojure

16 The End “I call it my billion-dollar mistake. It was the invention of the null reference in At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” --Tony Hoare