Docsity

Java Programming: Lecture 2 - History of Programming Languages and Java Basics, Assignments of Computer Science

Partial preview of the text

Download Java Programming: Lecture 2 - History of Programming Languages and Java Basics and more Assignments Computer Science in PDF only on Docsity!

Java Review

Lecture 2

CS2110 Fall 2008

Announcements

 Assignment 1 has been posted

 Due Wednesday, September 10, 11:59pm  Materials available in CMS

 Check that you are in CMS

 Report any problems to your Section TA (email is fine)

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

this week (well before the assignment is due)

 Available help

 Consulting will start very soon—watch for announcements  Instructor & TA office hours are in effect

 Check daily for announcements

http://courses.cs.cornell.edu/cs

Today — A Smorgasbord

 A brief (biased) history of programming languages

 Review of some Java/OOP concepts

 Java tips, trick, and pitfalls

 Debugging and experimentation

Machine Language

 Used with the earliest electronic computers (1940s)  Machines use vacuum tubes instead of transistors  Programs are entered by setting switches or reading punch cards  All instructions are numbers  Example code 0110 0001 0000 0110 Add Reg1 6  An idea for improvement  Use words instead of numbers  Result: Assembly Language

High-Level Language

 Idea: Use a program (a compiler or an interpreter ) to convert high-level code into machine code  Pro  Easier for humans to write, read, and maintain code  Con  The resulting program will never be as efficient as good assembly-code  Waste of memory  Waste of time  The whole concept was initially controversial  FORTRAN (mathematical FORmula TRANslating system) was designed with efficiency very much in mind

FORTRAN

 Initial version developed in 1957 by IBM  Example code C SUM OF SQUARES ISUM = 0 DO 100 I=1, ISUM = ISUM + I*I 100 CONTINUE  FORTRAN introduced many high-level language constructs still in use today  Variables & assignment  Loops  Conditionals  Subroutines  Comments

COBOL

 COBOL =

COmmon Business Oriented Language  Developed by the US government (about 1960)  Design was greatly influenced by Grace Hopper  Goal: Programs should look like English  Idea was that anyone should be able to read and understand a COBOL program  COBOL included the idea of records (a single data structure with multiple fields , each field holding a value)

Simula & Smalltalk

 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  These languages included  Classes  Objects  Subclasses & Inheritance

 Classes and objects

 Static vs instance fields and methods

 Primitive vs reference types

 Private vs public vs package

 Constructors

 Method signatures

 Local variables

 Arrays

 Subtypes and Inheritance, Shadowing

We will assume you already know

something about ...

Constructors

 Called to create new instances of a class

 Default constructor initializes all fields to default values

( 0 or null)

class Thing { int val; Thing(int val) { this.val = val; } Thing() { this(3); } } Thing one = new Thing(1); Thing two = new Thing(2); Thing three = new Thing();

16 class Widget { static int nextSerialNumber = 10000; int serialNumber; Widget() { serialNumber = nextSerialNumber++; } public static void main(String[] args) { Widget a = new Widget(); Widget b = new Widget(); Widget c = new Widget(); System.out.println(a.serialNumber); System.out.println(b.serialNumber); System.out.println(c.serialNumber); } }

Static vs Instance Example

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

The main Method

public static void main(String[] args) {

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

 Refer to my static and instance fields & methods by (unqualified) name:  serialNumber  nextSerialNumber  Refer to static fields & methods in another class using name of the class  Widget.nextSerialNumber  Refer to instance fields & methods of another object using name of the object  a.serialNumber  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)  If an object must refer to itself, use^ this