Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Programming Languages:
Part 1
Robert M. Dondero, Ph.D.
Princeton University
1
Objectives
You will learn/review:
Subsets of C, Java, and Python...
That are appropriate for COS 333...
Through example programs
Example 1 in C, Java, Python
Example 2 in C, Java, Python
…
2
Objectives
Note:
Comprehensive coverage is impossible!
Please supplement with reading
3
Objectives
Specifically:
Overview
Program structure
Building and running
Multiple functions/methods
Using library code
4
C Overview
Who:
AT&T Bell Labs
Dennis Ritchie
When: 1972
Why: Build Unix
5
C Overview
Characteristics:
Low level
Strongly typed
Minimal standard library
Two-word description:
Fast: Close to the OS and hardware
Eccentric: Unless you know the underlying
assembly/machine lang
6
Why Study C?
Why study C?
The world's most popular/influential lang
Illustrates some of the course's material
Provides strong contrast with other langs
I know it
You know it from COS 217
We'll do a quick review
7
Java Overview
Who:
Sun Microsystems
Team led by...
James Gosling
8
Java Overview
When: 1990
Why:
Embedded systems ("Oak"), and then...
"The language of the Web", and then...
Robust portable server-side software
9
Java Overview
Characteristics:
Medium level
Strongly typed
Large standard library
Two-word description:
Rich: Large language and standard library
Verbose: I/O is especially so
10
Why Study Java?
Why study Java?
Elegant and popular
Illustrates most of the course's material
I know it
You know it from COS 126/226
But maybe not some of the advanced
features
We'll do a quick overview
11
Python Overview
Who:
Guido Van Rossum
12
Separated at Birth?
13
Python Overview
When: ~1990
Why:
"I was looking for a 'hobby' programming project that would
keep me occupied during the week around Christmas. My
office … would be closed, but I had a home computer, and
not much else on my hands. I decided to write an interpreter
for the new scripting language I had been thinking about
lately: a descendant of ABC that would appeal to Unix/C
hackers. I chose Python as a working title for the project,
being in a slightly irreverent mood (and a big fan of Monty
Python's Flying Circus)." – Van Rossum
14
Python Overview
Characteristics:
High level
Weakly typed
No variable declaration statements
"Scripting language"
Rich standard library
But inconsistent programming conventions
15
Python Overview
Two-word description:
Expressive: Can accomplish much work with
little code
Immature: Still evolving; ill-documented
16
Why Study Python?
Why study Python?
Reasonably elegant and getting more popular
Illustrates most of the course's material
Esp. vs. Perl
Python/Django is popular
I'm new to it; I want to learn it
You also are new to it???
Thus the first part of the course won't be
entirely review???
17
Why Study that Combination?
Among “high level” languages:
Good for
computers
C
Java
Python
Good for
pgmmers
The three languages cover the spectrum
well
18
Our Approach
Approach: sequence of example programs
For each example:
Examine/run code
Note important features
Generalize
Incidentally...
No function/method comments
Bad style but saves paper
19
"Hello World" Programs
Illustrate
Program structure
Building and running
20
"Hello World" in C
See hello.c
Overall structure
Comments
#include preprocessor directive
Definition of main() function
printf() function
String literal ("…")
21
"Hello World" in C
Generalizations:
A C program consists of functions
Exactly one must be named "main"
22
"Hello World" in C
Building and executing:
hello.c
C preprocessor/compiler/
assembler/linker
At
run-time
OS
hello
(executable
binary code)
Source code is portable (to some extent)
Executable binary code is not portable
23
"Hello World" in C
Long procedure:
File: hello.c
Preprocess
$ gcc -Wall -ansi -pedantic -E
hello.c > hello.i
File: hello.i
Compile
$ gcc -Wall -ansi -pedantic -S
hello.i
24
"Hello World" in C
File: hello.s
File: hello.o
Assemble
$ gcc -Wall -ansi -pedantic -c
hello.s
Link
$ gcc -Wall -ansi -pedantic
hello.o -o hello
File: hello
Run
$ hello
25
"Hello World" in C
Short procedure:
File: hello.c
Preprocess, compile, assemble, link
$ gcc -Wall -ansi -pedantic
hello.c -o hello
File: hello
Run
$ hello
26
"Hello World" in Java
See Hello.java
Overall structure
Comments
Class definition
Definition of main() method
"static" => class level, not object level
System.out.println() method
String literal ("…")
27
"Hello World" in Java
Generalizations:
A program consists of classes
Define one public class per file
Can define other non-public classes in
same file
A class consists of methods (and fields)
28
"Hello World" in Java
Building and executing:
Hello.java
Java compiler
OS
At
run-time
Java interpreter (JVM)
Hello.class
(bytecode)
Source code is portable
Bytecode is portable
29
"Hello World" in Java
Procedure:
File: Hello.java
Compile
$ javac Hello.java
File: Hello.class
Run
$ java Hello
30
"Hello World" in Python
See hello1.py
Overall structure (minimal!)
#!/usr/bin/env python
Commands OS to...
Use the Python interpreter
Search the PATH env var to find it
Comments
print statement
String literal ('…')
No semicolons!!!
31
"Hello World" in Python
Generalizations:
Program consists of statements (and other
things; stay tuned)
String literal: '…' or "…"
32
"Hello World" in Python
Building and executing:
OS
At
run-time
Python interpreter
hello1.py
Python compiler
hello1.pyc
(bytecode)
Source code is portable
Bytecode is portable
33
"Hello World" in Python
Procedure 1 (works with/without #! line)
$ python hello1.py
Procedure 2 (works only with #! line)
$ chmod 700 hello1.py
$ hello.py
Procedure 3 (works with/without #! line)
$ python
>>> import hello1
Procedure 3 is valuable for learning
34
"Hello World" in Python
See hello2.py
Function definitions and calls
__name__ == '__main__'
True iff this module is the "main" module
True iff this module is compiled/interpreted
directly from the command-line
False iff this module is loaded into
interpreter by another module
Indentation matters!!!
35
"Hello World" in Python
Generalizations:
Program consists of statements and functions
Indentation indicates nesting
Better to avoid global code
Global code can hide errors in local code
36
"Hello World" in Python
Procedure 1 (works with/without #! line)
python hello2.py
Procedure 2 (works only with #! line)
chmod 700 hello2.py
hello2.py
37
"Hello World" in Python
Procedure 3 (works with/without #! line)
$ python
>>> import hello2
Makes all names defined in the hello2
module accessible within this (the main)
module
>>> hello2.main()
Can use those names using "hello2." prefix
38
"Hello World" in Python
Procedure 4 (works with/without #! line)
$ python
>>> from hello2 import main
Imports "main" from the hello2 module into
this (the main) module
>>> main()
Can use "main" as if it were defined in this
module
39
"Square" Programs
Illustrate:
Multi-function/method programs
Function parameters
40
"Square" in C
See square1.c
One source code file defines multiple
functions
Parameters are passed by value
Compiler does one pass only
Compiler must see function definition (or
declaration) before function call
41
"Square" in C
See square2.c
Function declaration
Compiler must see function definition or
declaration before function call
42
"Square" in Java
See Square.java
One source code file (one class) defines
multiple methods
Parameters are passed by value
Compiler does two passes
Can define methods in any order
43
"Square" in Python
See square1.py
One source code file defines multiple
functions
Weakly typed
Parameters are typeless
Compiler/interpreter does one pass only
Compiler/interpreter must see function def
before function call
44
"Square" in Python
However...
Python does not compile/interpret code
until necessary
45
"Square" in Python
See square2.py
OK to define sqr() after main()
See square3bad.py
Compiler/interpreter attempts to interpret call
of sqr() before seeing def of sqr()
Error
"Square" in Python
See square4bad.py
Compiler/interpreter does not attempt to
interpret call of non-existing function
No error detected!
Dangerous!!!
"SquareRoot" Programs
Illustrate:
Using library code
48
"SquareRoot" in C
See squareroot.c
#include <math.h>
Provides compiler with declaration of sqrt()
Allows compiler to check calls of sqrt()
49
"SquareRoot" in C
To build:
$ gcc -Wall -ansi -pedantic
squareroot.c -o squareroot -lm
-lm commands linker to search libm.a for
function defs
Required on many/most systems, but not
on ours
Experiment:
Remove #include directive
What happens?
50
"SquareRoot" in Java
See SquareRoot1.java
Uses full class name in code
Fine, but sometimes awkward
See SquareRoot2.java
import java.lang.Math;
Commands compiler to resolve "Math" to
"java.lang.Math"
Actually, not necessary: "java.lang.*" is
imported by default
51
"SquareRoot" in Python
See squareroot1.py
import math
Makes all names defined in the math
module accessible within this (the main)
module
Can use those names using "math." prefix
Bad: Sometimes awkward to use prefix
Good: No conflicts with names in this module
52
"SquareRoot" in Python
See squareroot2.py
from math import sqrt
Imports "sqrt" from the math module into
this module
Can use "sqrt" as if it were defined in this
module
Good: No need to specify prefixes
Bad: Potential name conflicts
New def overwrites old def
53
Summary
We have covered:
In Java, C, and Python...
Program structure
Building and running
Multiple functions/methods
Using library code
54