A programming language defines a virtual computation environment
Or, a virtual computation environment is assumed, and a programming language
is designed to describe computations in that VCE
It's a chicken-and-egg thing
The ML virtual computation environment
Very much like math -- based on expressions and functions
Always evaluating some expression in order to obtain some value
Very strongly typed -- every value had a type, and only the user could
force conversions
e.g., real(4) -> 4.0:real, round(4.6) -> 5:int
Identifiers could refer to values (bound to values)
they could be "reassigned" to different values (even of different types)
but the old value could be used if some existing function referred to it
Aggregate forms of values: tuples and lists
Recursion to do the same thing repeatedly
This VCE is an example of a functional computation environment
A function is a relationship, as well as a blueprint for computation --
being vs. doing
What if you are interested in just doing something, rather than
in computing some value?
For example, you just want to print out "My name is Bob"
ML can do this, but it is not its strength, and in some sense violates
the idea of a pure functional computation environment
So, some virtual computation environments are centered around the idea
of doing rather than computing an expression, or "being"
In Computer Science, we call such computation imperative (from the
English meaning of: a command, i.e., "do this")
And a programming language for such a VCE is an imperative programming
language
Think of the kitchen example -- recipes are generally written in an imperative
style
Java is an imperative programming language
Rather than just expressions, now we have statements
These statements are executed (as "commands") in order
Kind-of like the let in ML, except a statement does not have to
result in a value, it might just do something
Values are stored in explicit locations, which are named, and called
variables
Variables are then assigned values, unlike ML, where identifiers
were bound to values
Assignment is a key concept in imperative programming languages
Variables can only store one value at a time, so unlike ML, a previous
value is "erased" once a new value is assigned to a variable
Variables are typed now, not just values. Variables can only store a value
of a specific type
Has functions, but also procedures -- which are named like functions,
have a parameter, but don't return a result (they just "do something")
In Java, both functions and procedures are also called methods
Example Java Program:
class Hello
{
public static void main( String[] Args )
{
System.out.println("Hello World.");
}
}
All Java programs are made up of modules, called classes. There
are built-in classes, such as System, and this program has one user
defined class, called Hello
One, and only one, user-defined class contains a procedure called main
This is where the Java program starts
This program has just one statement, which is a procedure call (like a
function call), which calls the procedure println in the module
(class) out, which is in the module (class) System.
Curly braces {} are used in many places to group statements; the class
definition is all inside curly braces, the procedure body is all inside
curly braces, and we will see more uses for curly braces
Running Java
Need to write your program, using emacs or some other editor, and save
it in a file
That file should be named with the class name that contains the main
procedure, and have an ending of .java
Our sample program should be in a file called Hello.java
Then, we run the command javac on this file, like "javac
Hello.java"
If our program has no syntax errors, this compiles our program into
an internal representation, call Java bytecode, which is put into
a file named for the class containing main, and given an ending
of .class
Our sample program would yield the file Hello.class (which
is why we should name the source file Hello.java)
We then use the Java interpreter, which is the command java,
to run our program -- and we just tell it the name of the class containing
the main procedure
The command to run our sample woud be "java Hello"