The Scala API Application Programmer’s Interface.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Strings Testing for equality with strings.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Fundamental Programming Structures in Java: Strings.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
JAVA LIBRARY CLASSES CITS Main concepts to be covered Using library classes: String, Math, Color Reading documentation Java 7 API is available.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Week71 APCS-AB: Java Control Structures October 17, 2005.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Python
Recitation 2 Main Method, API & Packages, Java Basics.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Java Data Types Data types, variable declaration, and initialization.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
Week1 Using the Library (the Java API) Classes are grouped together in packages –To use a class you have to know which package it is in –Every package.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
CSC Programming I Lecture 9 September 11, 2002.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
More Sophisticated Behavior
Lecture 19 Strings and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Introduction to.
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 6 The while Statement
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Advanced String handling
Part a: Fundamentals & Class String
T. Jumana Abu Shmais – AOU - Riyadh
Java – String Handling.
CS2011 Introduction to Programming I Strings
Strings in Java.
Exam Prep.
In Java, strings are objects that belong to class java.lang.String .
What We Want To Do User enters: Mary Smith
Presentation transcript:

The Scala API Application Programmer’s Interface

Reinventing the wheel When you learn a foreign language, you learn both the grammar (syntax) and the vocabulary If you learn just a few hundred words, you can usually make yourself understood (though you may not understand other people), but that doesn’t make you fluent in the language If you were writing in the foreign language, you would use a dictionary to express yourself better When you learn a programming language, you learn both the syntax and the libraries (“vocabulary”) You can write (almost) any program using only the syntax if (me == "Tarzan") you = "Jane" You are not “speaking” Scala, you are writing it Learn to use the libraries! 2

Converting to lowercase scala> :paste // Entering paste mode (ctrl-D to finish) val s = "Letters AND digits, 1 2 3!" var lc = "" for (ch = 'A' && ch <= 'Z') { lc = lc + (ch - 'A' + 'a').toChar } else { lc = lc + ch } } (Entered ctrl-D) // Exiting paste mode, now interpreting. s: String = Letters AND digits, 1 2 3! lc: String = letters and digits, 1 2 3! scala> val lc2 = s.toLowerCase lc2: String = letters and digits, 1 2 3! 3

Discovering methods in the REPL In the REPL, you can hit a tab after a period to find out what methods are available scala> "abc". TAB + asInstanceOf charAt codePointAt codePointBefore codePointCount compareTo compareToIgnoreCase concat contains contentEquals endsWith equalsIgnoreCase getBytes getChars indexOf intern isEmpty isInstanceOf lastIndexOf length matches offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring toCharArray toLowerCase toString toUpperCase trim Or you can type a few letters after the period, then hit tab: scala> "abc".to TAB toCharArray toLowerCase toString toUpperCase Notice the + in the first list above 4

Operators and methods Operators are things like +, *, <=, &&, etc. In Scala, operators are really methods, and are in the API along with all the other methods Here’s a neat trick: If a method takes exactly one argument (in addition to the object you are talking to), you can use it like a binary operator scala> 5.max(7) res0: Int = 7 scala> 5 max 7 res1: Int = 7 You can use this trick to save typing and make your code more readable Less usefully, you can treat binary operators as methods scala> res2: Int = 12 scala> 5.+(7) res3: Int = 12

The Scala API Scala comes with a LARGE library of classes! Each class may contain many, many methods Scala can also use the classes that come with Java Java also provides lots of classes and methods Altogether there are thousands of methods you can use! The Scala API (Application Programmer’s Interface) is a large collection of web pages that tells you what classes and methods are available Access it online at To have it available when you are not online, download the API from 6

Using the Scala API Here’s the good news: Just about any common task you can think of, the wizards at Sun, Oracle, and Typesafe have already written (and debugged!) it for you Here’s the bad news: It’s huge! It’s scary! It’s intimidating! No, really. It is. Class, object, and method descriptions often use mysterious words and syntax that you don’t yet understand Some of what you want isn’t in the Scala API, it’s in the Java API Some methods are poorly documented or not documented at all Nevertheless, you have to learn to use the API at least a little bit 7

This is where you start 8

Enter a class/object name to go there 9

Difference between “C” and “O” Numbers have types: 5 is an Int, 5.0 is a Double Types are defined by classes 5.0 is an object of the class Double The methods in “C” are the methods that can be applied to individual Double s, because they belong to the class Double scala> 5.75.round res5: Long = 6 Double is itself an object It isn’t a number, it’s an object that describes certain kinds of numbers—those we think of as “doubles” The methods in “O” are those that can be applied to Double itself scala> Double.MaxValue res7: Double = E308

Scroll down to find what you need 11

Don’t panic! Using the Scala API can be intimidating It’s big! And a lot of it is confusing or incomprehensible The trick is to ignore everything you don’t understand Getting comfortable with the API is an essential part of learning to program in Scala (or any other language) The code in the libraries has been written by experts Using the library methods makes your code simpler and easier to read scala> val absrad = if (rad < 0) -rad else rad absrad: Double = scala> val absrad = rad.abs absrad: Double =

Using the Java API 13

Where to look The following packages are imported by default; you don’t have to do anything: scala._ (meaning everything from the scala package) Contains operations on numbers and various other things java.lang._ (meaning everything from the java.lang package) Most useful for the operations on strings These packages are imported by default because you will need them in virtually every program Later on, you may find you need to important certain other packages For example, if you need trigonometric functions, import scala.math If you want to write a graphical user interface, see scala.swing 14

15 The End