Java Lecture 1

Back to the beginning
  1. A programming language defines a virtual computation environment
  2. Or, a virtual computation environment is assumed, and a programming language is designed to describe computations in that VCE
  3. It's a chicken-and-egg thing
  4. The ML virtual computation environment
    1. Very much like math -- based on expressions and functions
      1. Always evaluating some expression in order to obtain some value
    2. Very strongly typed -- every value had a type, and only the user could force conversions
      1. e.g., real(4) -> 4.0:real, round(4.6) -> 5:int
    3. Identifiers could refer to values (bound to values)
      1. they could be "reassigned" to different values (even of different types)
      2. but the old value could be used if some existing function referred to it
    4. Aggregate forms of values: tuples and lists
    5. Recursion to do the same thing repeatedly
    6. This VCE is an example of a functional computation environment
    7. A function is a relationship, as well as a blueprint for computation -- being vs. doing
  5. What if you are interested in just doing something, rather than in computing some value?
  6. For example, you just want to print out "My name is Bob"
    1. ML can do this, but it is not its strength, and in some sense violates the idea of a pure functional computation environment
  7. So, some virtual computation environments are centered around the idea of doing rather than computing an expression, or "being"
    1. In Computer Science, we call such computation imperative (from the English meaning of: a command, i.e., "do this")
    2. And a programming language for such a VCE is an imperative programming language
    3. Think of the kitchen example -- recipes are generally written in an imperative style
  8. Java is an imperative programming language
    1. Rather than just expressions, now we have statements
    2. These statements are executed (as "commands") in order
      1. Kind-of like the let in ML, except a statement does not have to result in a value, it might just do something
    3. Values are stored in explicit locations, which are named, and called variables
    4. Variables are then assigned values, unlike ML, where identifiers were bound to values
      1. Assignment is a key concept in imperative programming languages
    5. 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
    6. Variables are typed now, not just values. Variables can only store a value of a specific type
    7. Has functions, but also procedures -- which are named like functions, have a parameter, but don't return a result (they just "do something")
    8. In Java, both functions and procedures are also called methods
  9. Example Java Program:
    1. class Hello
      {
         public static void main( String[] Args )
         {
            System.out.println("Hello World.");
         }
      }
  10. 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
  11. One, and only one, user-defined class contains a procedure called main
    1. This is where the Java program starts
  12. 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.
  13. 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
  14. Running Java
    1. Need to write your program, using emacs or some other editor, and save it in a file
    2. That file should be named with the class name that contains the main procedure, and have an ending of .java
    3. Our sample program should be in a file called Hello.java
    4. Then, we run the command javac on this file, like "javac Hello.java"
    5. 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
    6. Our sample program would yield the file Hello.class (which is why we should name the source file Hello.java)
    7. 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
      1. The command to run our sample woud be "java Hello"