CS 1110/1111: Introduction to Programming

Lecture 31

Announcements

Talking Points

None of this material is in the textbook.

Bits and Type

In memory all we store are high- and low-voltage bits. If we take a group of n bits we can put them into 2n configurations.

double values are stored as 64 bits, for 18,446,744,073,709,551,616 possible double values.
long values are also stored as 64 bits, for 18,446,744,073,709,551,616 possible long values.
How does Java tell if a particular group of 64 bits is a double or a long (or 2 32-bit ints, or 4 16-bit chars…)? By remembering what type it put in those bits.

The War between Filing Systems

There are lots of numbers in your life. Social security numbers, student ID numbers, SIS person numbers, telephone numbers, zip codes, … How do you know which number is which?

Option 1: remember. This number is on the post-it beside my bed, which is where I wrote my student ID number, so that must be what it is.

Option 2: annotate. This piece of paper says SSN: 012345678 so it must by my SSN, not my telephone number.

In programming we call the first static typing and the latter dynamic typing. People argue about which one is better heatedly.

Java is statically typed

In Java, each location I can store bits (i.e., each variable) has a type. When I say int x I'm telling Java remember, whatever bits I put in this box will be an int, not a float.

Others are dynamically typed

Some languages variables can store anything, but values keep around their type. So we could do something like

x = 3;
x = "hi";
x = 11.2;
x = new Greengrocer("Dana");

In each case the dynamically-typed language would put into the box x a type-value pair like int, 00...011 instead of just 00...011

More on typing

Java is strongly typed. That is, if I set some bits to be an int I can only ever use them to be an int; I can't break the type system and read the bytes like a float instead.

As a broad over-generalization, statically typed languages have less chance of getting the programmer confused about what a variable means and almost always run more quickly than dynamically typed languages. Dynamically typed languages almost always require less typing to write the same program. The current trend is to use statically-typed languages for large programs and dynamically-typed languages for smaller ones.

What makes a program good?

Given a choice, which would you prefer to do?

We'll talk more about this question next class.

From Lecture

002 (1pm)

LanguagesProgrammingTypes
JavaYesStatic
C++YesStatic
PythonYesDynamic
C#YesStatic
RubyYesDynamic
HTMLNo
HTML5No
SpinYesDynamic
LabviewYesDynamic
FortranYesStatic
BasicYesDynamic
CSSNo
PowershellYesDynamic
JavascriptYesDynamic
PascallYesStatic
QPL6No
PerlYesDynamic
int x = 3
x = x + 3;
String s = "h"
s = s + s;
— stack memory —
typenamevalue
intx6
Strings@787
— heap memory —
addressvalue
3546h
787hh

x = 3
y = "hi"
x = y
namevalue
xstring:"hi"
ystring:"hi"

001 (3pm)

StaticDynamicMarkupOther
JavaPythonHTMLAndroid = Java
C++RubyCSSHTML5 = HTML + CSS + Javascript, recent version
C#VB
CobolJavascript
Labview CPHP
Labview
Matlab
store type with value
store value and remember type based on location
int x = 3
String s = "h"
s = s + s;
— stack memory —
typenamevalue
intx3
Strings@314
— heap memory —
addressvalue
7878h
314hh

x = 2
y = "hi"
x = y
namevalue
xstring:"hi"
ystring:"hi"
Copyright © 2014 by Luther Tychonievich. All rights reserved.