None of this material is in the textbook.
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 int
s, or 4 16-bit char
s…)?
By remembering what type it put in those bits.
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.
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
.
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
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.
Given a choice, which would you prefer to do?
We'll talk more about this question next class.
Languages | Programming | Types |
---|---|---|
Java | Yes | Static |
C++ | Yes | Static |
Python | Yes | Dynamic |
C# | Yes | Static |
Ruby | Yes | Dynamic |
HTML | No | — |
HTML5 | No | — |
Spin | Yes | Dynamic |
Labview | Yes | Dynamic |
Fortran | Yes | Static |
Basic | Yes | Dynamic |
CSS | No | — |
Powershell | Yes | Dynamic |
Javascript | Yes | Dynamic |
Pascall | Yes | Static |
QPL6 | No | — |
Perl | Yes | Dynamic |
int x = 3 x = x + 3; String s = "h" s = s + s;
— stack memory — | ||
---|---|---|
type | name | value |
int | x | 6 |
String | s | @787 |
— heap memory — | |
---|---|
address | value |
3546 | h |
787 | hh |
x = 3 y = "hi" x = y
name | value |
---|---|
x | string:"hi" |
y | string:"hi" |
Static | Dynamic | Markup | Other |
---|---|---|---|
Java | Python | HTML | Android = Java |
C++ | Ruby | CSS | HTML5 = HTML + CSS + Javascript, recent version |
C# | VB | ||
Cobol | Javascript | ||
Labview C | PHP | ||
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 — | ||
---|---|---|
type | name | value |
int | x | 3 |
String | s | @314 |
— heap memory — | |
---|---|
address | value |
7878 | h |
314 | hh |
x = 2 y = "hi" x = y
name | value |
---|---|
x | string:"hi" |
y | string:"hi" |