Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Java Review Interface, Casting, Generics, Iterator.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
Lecture 9 Concepts of Programming Languages
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Abstract Data Types and Encapsulation Concepts
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
OOP Languages: Java vs C++
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Inheritance in the Java programming language J. W. Rider.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Types in programming languages1 What are types, and why do we need them?
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Interfaces and Inner Classes
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Inheritance and Polymorphism
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
OOP Basics Classes & Methods (c) IDMS/SQL News
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Object-Oriented Concepts
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Continuing Chapter 11 Inheritance and Polymorphism
Lecture 9 Concepts of Programming Languages
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
The Building Blocks Classes: Java class library, over 1,800 classes:
Java Programming Language
Type Systems Terms to learn about types: Related concepts: Type
Subtype Substitution Principle
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Programming in Scala Chapter 1

Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically typed language.

All but the most trivial programs need some sort of structure. Put data and operations into some form of containers. Make these containers fully general, so that they can contain operations as well as data They are themselves are also values that can be stored in other containers, or passed as parameters to operations. Such containers are called objects. Object-oriented programming

Many languages admit values that are not objects, such as the primitive values in Java. Or they allow static fields and methods that are not members of any object. These deviations from the pure idea of object-oriented programming look quite harmless at first, but they have an annoying tendency to complicate things and limit scalability. Scala is an object-oriented language in pure form. –Every value is an object. –Every operation is a method call. –For example, when you say in Scala, you are actually invoking a method named + defined in class Int. Like new Integer(1).plus(new Integer(2)) Object-oriented programming (cont.)

Functional programming: two big ideas Functions are first-class values—with the same status as an integer or a string. –You can pass functions as arguments, return them as results, or store them in variables. –You can define functions without giving them a name. No side-effects. Inputs are mapped to outputs, but no data is changed. Instead new values are generated. Like Strings in Java, values are immutable. Scala encourages functional programming but doesn’t require it.

Why Scala? rather than Haskell or OCaml or Ruby, Python, or Grooby Compatible with Java. Compiles to the JVM. –But so does JRuby and Groovy. Interoperable with Java code. Scala code can –call Java methods, –access Java fields, –inherit from Java classes, –implement Java interfaces. –Scala makes heavy behind the scenes use of Java libraries. –Extends some, e.g. String. How? –String s = “123”; s.toInt(); -- How is this possible? –Declared implicit conversion. Since toInt() is not defined in String, convert to RichString.

Scala is concise Typical savings of 50%. // this is Java class MyClass { private int index; private String name; public MyClass(int index, String name) { this.index = index; this.name = name; } new myClass(3, “abc”) // this is Scala class MyClass(index: Int, name: String) new myClass(3, “abc”) // Instance variables and constructor defined implicitly

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; } // this is Scala val nameHasUpperCase = name.exists(_.isUpperCase) // exists() causes isUpperCase() to be applied to each // element in name until one that satisfies it is found. // Note that isUpperCase() is a function being passed as // an argument to exists(). Could do something similar // in Java but would have to build a class. Most people find // it too much trouble to bother.

Scala is statically typed Strong typing is very important in functional programming –Most scripting like Ruby, Python, etc. are dynamically typed, which is supposed to be one of their advantages. –Static typing makes programming much safer. –But sometimes types declarations become a burden. –Scala’s type inferencing mechanism minimizes the number of type declarations required. It’s not uncommon for user code to have no explicit types.