



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the history of programming languages, focusing on machine language, assembly language, high-level languages such as fortran, algol, cobol, and java. It covers the evolution of programming languages, their features, and common pitfalls.
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Another session tonight 4-6 in B7 Upson tutorial & solutions also available online
Report any CMS problems to me ASAP
Office hours on course web site
Also monitor newsgroup (working??)
y Used with the earliest electronic computers (1940s) Machines use vacuum tubes instead of transistors y Programs are entered by setting switches or reading punch cards
y All instructions are numbers
y Example code 0110 0001 0000 0110 Add Reg1 6
y An idea for improvement Use “words” instead of numbers Result: Assembly Language
y Idea: Use a program (an assembler ) to convert assembly language into machine code y Early assemblers were some of the most complicated code of the time (1950s) y Example code ADD R1 6 MOV R1 COST SET R1 0 JMP TOP
Typically, an assembler uses 2 passes
y Idea for improvement Let’s make it easier for humans Result: high-level languages
y Idea: Use a program (a compiler or an interpreter ) to convert high-level code into machine code
y Pro Easier for humans to write, read, and maintain code y Con The resulting program will never be as efficient as good assembly-code Waste of memory Waste of time
y The whole concept was initially controversial FORTRAN (mathematical FORmula TRANslating system) was designed with efficiency very much in mind
y Initial version developed in 1957 by IBM
y Example code C SUM OF SQUARES ISUM = 0 DO 100 I=1, ISUM = ISUM + I*I 100 CONTINUE
y FORTRAN introduced many high-level language constructs still in use today Variables & assignment Loops Conditionals Subroutines
y ALGOL = ALGOrithmic Language
y Developed by an international committee
y First version in 1958 (not widely used)
y Second version in 1960 (widely used)
y ALGOL 60 included recursion Pro: easier to design clear, succinct algorithms Con: too hard to implement; too inefficient
y Sample code comment Sum of squares begin integer i, sum; for i:=1 until 10 do sum := sum + i*i; end
y COBOL = COmmon Business Oriented Language y Developed by the US government (about 1960) Design was greatly influenced by Grace Hopper y Goal: Programs should look like English
y Sample code MULTIPLY B BY B GIVING B-SQUARED. MULTIPLY 4 BY A GIVING FOUR-A. MULTIPLY FOUR-A BY C GIVING FOUR-A-C. SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1. COMPUTE RESULT-2 = RESULT-1 ** .5.
y COBOL included the idea of records (a single data structure with multiple fields , each field holding a value) y Immensely popular language
y These languages introduced and popularized Object Oriented Programming (OOP) Simula was developed in Norway as a language for simulation in the 60s Smalltalk was developed at Xerox PARC in the 70s
y These languages included Classes Objects Subclasses & Inheritance
y Developed in 1972 by Dennis Ritchie at Bell Labs
y Idea was a high-level language with nearly the power of assembly language Originally used to write UNIX
y Pro: fast and powerful, simple syntax y Con: often too powerful C trusts the programmer Very difficult to write secure, stable code
y Very widely used Many descendents: C++, C#, Objective C, etc.
class Thing { int val;
boolean setVal(int v) { int val = v; } }
local variable shadows field
class Thing { int val;
boolean setVal(int val) { val = 10 * val; } }
class Thing { int val;
boolean setVal(int _val) { val = 10 * _val; } }
Including built-in Java classes
Parameters passed to program on command line
A class method; don’t need an object to call it
Can be called from anywhere
No return value
y Refer to fields & methods in own class by (unqualified) name serialNumber, nextSerialNumber
y Could also use this reference (but rarely needed) this.serialNumber, this.nextSerialNumber
y Refer to static fields & methods in another class using name of the class Widget.nextSerialNumber
y Refer to instance fields & methods in another class using name of the object a.serialNumber
y Example System.out.println(a.serialNumber) out is a static field in class System The value of System.out is an instance of a class that has an instance method println(int)
But all methods must have different signatures The signature of a method is its name plus types of its parameters
There are 9 of them: valueOf(boolean); valueOf(int); valueOf(long); ... Parameter types are part of the method’s signature Return type is not part of the signature
y Primitive types int, short, long, float, byte, char, boolean, double Efficiently implemented by storing directly into variable Not considered an Object by Java: “unboxed” x^ true
char 2 bytes [0, 65535]
boolean {true, false}
double 8 bytes Approx. ±[4.94e-324 to 1.80e+308]
float 4 bytes Approx. ±[1.40e-45, 3.40e+38]
[-9,223,372,036,854,775,808, 9,223,372,036,854,775,807]
long 8 bytes
int 4 bytes [-2,147,483,648, 2,147,483,647]
short 2 bytes [-32768, 32767]
byte 1 byte [-128, 127]
Type Size Range
y Reference types Objects and arrays String, int[], HashSet Usually require more memory Can have special value null Can compare null with ==, != Generates NullPointerException if you try to dereference it
x
y == tests whether variables hold identical values
y Works fine for primitive types
y For reference types (e.g., String), you usually want to use equals() == means “they are the same object” Usually not what you want!
y To compare object contents , define an equals() method boolean equals(Object x);
y Two different strings with value "hello" x = "hello"; y = "hello"; x == y?
x y
"hello" (^) "hello"
"xy" == "xy" "xy".equals("xy")
"xy" == "x" + "y" "xy".equals("x" + "y")
"xy" == new String("xy") "xy".equals(new String("xy"))
y The String class is special It’s the only class that is allowed to overload operators E.g. No other class is allowed to do this
String objects are immutable : it is not possible to change the contents of a String object after it has been constructed If the same string literal appears multiple times in a program, the compiler might create only one object as an optimization
String s = "x" + "y"
Widget w = w1 + w
true/false?
true/false?
false
true
true
true
y Widget a; just creates a reference to a Widget object But not a Widget object! a is automatically initialized to null So a.serialNumber throws a NullReferenceException
y Fix it by creating a new object: Widget a = new Widget();
class Widget { public static void main(String[] args) { Widget a;
System.out.println(a.serialNumber); } }
class Widget { public int modelNumber;
public static void main(String[] args) { Widget widget1 = new Widget(); Widget widget2 = widget1;
widget1.modelNumber = 13; widget2.modelNumber = 14; System.out.println(widget1.modelNumber); System.out.println(widget2.modelNumber); } }
y Don't be afraid to experiment if you don't know how things work An IDE ( Interactive Development Environment ; e.g., DrJava or Eclipse) makes this easy
y Debugging Do not just make random changes, hoping something will work Think about what could cause the observed behavior Try to isolate the bug Use print statements combined with binary search Comment out unrelated parts of the program But make sure to keep backup copies of your code!! An IDE makes this easy by providing a Debugging Mode Can step through the program while watching chosen variables