A Brief History of Programming Languages and Java, Assignments of Computer Science

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-58w
koofers-user-58w 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Review
Lecture 2
CS2110 Summer 2008
Announcements
yJava Bootcamp
Another session tonight 4-6 in B7 Upson
tutorial & solutions also available online
yAssignment 1 has been posted and is due Friday,
June 27, 10pm
yCheck that you are in CMS after 1pm
Report any CMS problems to me ASAP
yIt’s really a good idea to start on A1 and check CMS
very soon (like today)
More Announcements
yAvailable help
Office hours on course web site
yCheck web page daily for announcements
Also monitor newsgroup (working??)
yLast day to register with Summer Sessions office is
tomorrow
Today
yA short, biased history of programming languages
yReview of some Java/OOP concepts
yCommon Java pitfalls
yDebugging and experimentation
Machine Language
yUsed with the earliest electronic
computers (1940s)
Machines use vacuum tubes instead of
transistors
yPrograms are entered by setting switches
or reading punch cards
yAll instructions are numbers
yExample code
0110 0001 0000 0110
Add Reg1 6
yAn idea for improvement
Use “words” instead of numbers
Result: Assembly Language
Assembly Language
yIdea: Use a program (an assembler) to convert
assembly language into machine code
yEarly assemblers were some of the most
complicated code of the time (1950s)
yExample code
ADD R1 6
MOV R1 COST
SET R1 0
JMP TOP
Typically, an assembler uses 2 passes
yIdea for improvement
Let’s make it easier for humans
Result: high-level languages
pf3
pf4
pf5

Partial preview of the text

Download A Brief History of Programming Languages and Java and more Assignments Computer Science in PDF only on Docsity!

Java Review

Lecture 2

CS2110 Summer 2008

Announcements

y Java Bootcamp

ƒ Another session tonight 4-6 in B7 Upson ƒ tutorial & solutions also available online

y Assignment 1 has been posted and is due Friday,

June 27, 10pm

y Check that you are in CMS after 1pm

ƒ Report any CMS problems to me ASAP

y It’s really a good idea to start on A1 and check CMS

very soon (like today)

More Announcements

y Available help

ƒ Office hours on course web site

y Check web page daily for announcements

ƒ Also monitor newsgroup (working??)

y Last day to register with Summer Sessions office is

tomorrow

Today

y A short, biased history of programming languages

y Review of some Java/OOP concepts

y Common Java pitfalls

y Debugging and experimentation

Machine Language

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

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

High-Level Language

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

FORTRAN

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

ALGOL

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

COBOL

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

Simula & Smalltalk

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

C

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; } }

A Common Pitfall

local variable shadows field

ƒ you would like to set the instance field val = v

ƒ but you have declared a new local variable val

ƒ assignment has no effect on the field val

class Thing { int val;

boolean setVal(int val) { val = 10 * val; } }

A Common Pitfall

Formal parameter shadows field

ƒ assignment has no effect on the field val

class Thing { int val;

boolean setVal(int _val) { val = 10 * _val; } }

A Common Pitfall

Formal parameter shadows field

ƒ assignment has no effect on the field val

Programs

y A program is a collection of classes

ƒ Including built-in Java classes

y A running program does computation using instances

of those classes

y Program starts with a main method, declared as:

public static void main (String[] args) {

...body...

} Method must be named^ main

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

Names

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)

Overloading of Methods

y A class can have several methods of the same name

ƒ But all methods must have different signatures ƒ The signature of a method is its name plus types of its parameters

y Example: String.valueOf(...) in Java API

ƒ 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

Types

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

Reference Types

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

= = vs equals( )

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"))

Fine print on Strings…

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

Another common pitfall

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); } }

Yet another pitfall

y The = operator copies references, not objects

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); } }

Experimentation and Debugging

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