Java Created in 1990 by Sun Microsystems. Free compiler from Sun, commercial from many vendors. We use free (Sun) Java on UNIX. Compiling and Interpreting...are processes of translating a high-level programming language to a lower-level language. Example: Java source-code to Byte code (or, Byte code to machine language). The implementation process (Java) 1. programmer types source code 2. programmer starts compiler 3. Java compiler translates source code to byte code 4. programmer starts Java runtime environment (JRE) 5. JRE translates byte code to machine code and executes immediately 6. program runs 7. programmers finds errors and starts from the beginning Example: Basic Programming Language Syntax Programming languages have very strict rules (unlike English or other human languages). The text book has a lot of information on this topic. Java source code (*.java-files) must adhere to such rules. We introduce a few syntax rules for Java (compare with textbook listing 1.1 on page 30). comments: are lines in the source code ignored by the compiler. The first two characters of a comment line must be //
identifiers: are strings that are use for naming program objects in your code. So far, the only use for an identifier we know is the name of the program (class). In the textbook listing, Lincoln is an identifier. naming conventions: identifiers must adhere to certain rules as well. The basic rules are: the first character of an identifier must be a letter (a...z,a...z) or an underscore (_) or a dollar sign ($), for the remaining characters digits (0...9) are allowed as well. The textbook contains a syntax diagram visualizing this concept (page 35). reserved words: are strings that the compiler uses, and which therefore cannot be used as identifiers (e.g., public, class). A list of Java reserved words is on page 35. white space: It is possible (and recommended) to nicely format source code through generous use of space characters and/or the breaking down of lines. Examples for well and badly formatted code (which is all the same to the compiler though) are in the textbook, Listings 1.1. through 1.3. basic program structure: every Java program (technically, every class file) at this point must begin with the words public class, followed by an identifier (the class name). The next line in all our simple programs for the next few weeks should be public static void main (String[] args). Compare with the textbook. parentheses (), brackets [], and braces {} are used for different purposes in programs. For now, we only need braces {}, to structure our code. After the ``main'' line (above), the compiler expects a pair of braces, which enclose your entire program. Further pairs of braces will be necessary sometimes, details will follow. Each opening brace must have a corresponding closing brace. Forgetting those is one of the most common programming errors. Syntax and Semantics A computer executes a program by following the programmer's instructions PRECISELY. A computer error is (almost) always a programmer's error. Computers do not make errors calculating your bank account balance. Programmers make errors describing the calculation to the computer. Computers do exactly what they are TOLD, not what the programmers WANTS. If programmer makes any mistake, computer usually produces an error. Three classes of errors: Syntax Error. Programmer breaks the basic rules of the programming language. compiler complains during compilation Runtime Error. Programmer overlooks some condition in the execution of a program (later). JRE complains during program execution
Logical Error. Computer does not complain, but program does something different from what the programmer WANTED. Elementary Programming in Java Printing to the Standard Output Window The standard output window is the window in which you type your commands (such as javac Xyz.java or java Xyz). The mode of communication in that window is called command line interaction since you type commands on one line. The computer responds with text output in the same way. The concept of a command line is very old, and in fact predates monitors (screens). Command lines were originally used with tele-typewriters (which are like electric typewriters with a computer connection). Therefore, the computer can only print one character at a time, from left to right, and when a line is full (typically a line has 80 characters), the computer will continue printing on the next line. The position where the computer is going to print next (on a typewriter that would be the position of the print head) is called the cursor position. The cursor is the (possibly blinking) line showing where a character will appear on the screen if you type one. For the next few weeks, any input or output, that is your communication with a running Java-program, will occur in the command line fashion. We will first discuss output, that is communication from the computer program to you. The First Programming Construct: println The simplest thing you can make a Java-program do is print a predefined text to the screen. This is accomplished as follows: System.out.println("Hello World."); The text you want to print must be enclosed in double quotes. The remainder of the statement must be typed exactly as above, including the semicolon at the end. A piece of text enclosed in quotes is called a String and consists of printable characters. The println statement can print strings but also numbers and results of arithmetic expressions. For example: System.out.println(3+4*5); which would print 23 because Java will observe elementary rules of arithmetics. The plus-character has two different meanings in Java: addition of numbers, and concatenation of strings. For example:
will print 7 and 34, respectively. System.out.println(3+4); System.out.println("3"+"4"); A small but important point is that the decision which meaning the plus-character has is made by the left operand, i.e., the symbol to the left of the +. That is, is a valid statement and will print 34. System.out.println("3"+4); Grouping elements with parentheses will change the order of computation (usually leftto-right) and also the interpretation of results. This is important in expressions that ``add'' more than two things. For example, the result of (3+4) is always the number 7, whereas the result of 3+4 could be something else, as in the following example: System.out.println("Three plus four is "+(3+4)); System.out.println("Three next to four is "+3+4); which would print Three plus four is 7 and Three next to four is 34, respectively. The latter statement is equivalent to System.out.println(("Three next to four is "+3)+4); because of the left-to-right evaluation in Java. One last minor point about strings is the impossibility to include a quote (") in a string, since strings are enclosed in quotes. As a workaround, Java uses the escape character (the backslash), used as follows: System.out.println("My name is John \"Buster\" Smith"); which would print My name is John "Buster" Smith. If you ever need to print a backslash, use two consecutive backslashes, as in "\\". The Second Programming Construct: print A variation of the println-statement is the print statement, which works exactly like println, with the exception that the cursor is not advanced to the next line at the end of the statement. For example: System.out.print("Ja"); System.out.print("va");
would print Java. If you use the print-statement, you should always use at least one println statement after the last print-statement (why?). Objects Object-Oriented Thinking Java is a so-called Object-Oriented Programming Language. The other well-known such language is C, but most current programming languages have at least some objectoriented concepts. There is no good definition for object, since it is a very general concept. We will explain objects through their properties. Objects have two major properties: 1. Objects can execute ``orders'' which are conveyed to them through ``messages''. 2. Objects can ``remember'' information about themselves. In the appropriate terminology: 1. Objects can execute methods which are invoked through method calls. 2. Objects can store information about themselves in the form of variables. For example, a cell in an Excel spreadsheet could be implemented (and most likely is implemented) as an object. Such an object could remember its content, color, font, font size, background color, number format etc., and display its content in the correct format when ordered to do so. That way, when changing the properties of a cell in Excel, one actually updates the variables in the cell-object, and when the spreadsheet is being recomputed by the computer (i.e., every time you change a number or a formula in the sheet), the cell does its share of the work by displaying its own content on the screen. One way of looking at objects is as independent agents that manipulate their own data when instructed to do so, and sometimes return the results of such manipulation in some form. The advantages of objects (over older programming techniques) are many and will become clearer throughout the course. Some initial thoughts are: 1. objects can be written once, and then used many times, in the same program or in other programs 2. objects are usually self-contained and can be developed independently from one another 3. objects can store information about themselves, and use such information at the appropriate time
4. objects can act as independent entities in a program, and even run in parallel (at the same time) with other objects on a single CPU. Some of these features also exist in non-object-oriented systems, but object-oriented systems have all these advantages at the same time. Note: a common abbreviation for ``object-oriented'' is ``OO'', such we can say ``Java is an OO-language''. Classes and Instances Many objects in a program are similar. In particular, the methods, i.e., the functionalities of many objects are equal. For example, all cells in a spreadsheet should be able to perform the same tasks. Therefore, it makes sense to write a ``program'' for only one cell in the spreadsheet, and simply ``load the program into memory'' as many times as the spreadsheet has cells. The program that one would write to describe the capabilities of an object is called a class. Whenever such a class is loaded into memory to become an active piece of a program, that copy in memory is called an instance. The process of loading a copy of a class into memory is called instantiation. Example: In our (slightly silly) example from the lecture, we had a number of assistant cooks who were taught the exact same skill set in cooking school. So each cook had the same skills, but still the cooks were not identical. For example, they have different social security numbers (object identifiers), and they stand in different places in the kitchen (memory locations). Also, each cook could remember different things (and do different things) from each other cook at any one time. So the ``assistant cook class'' is described by the common skill set of all these cooks (methods), plus the kinds of things they could possibly remember (variables). Each individual cook is an instance of the ``assistant cook class''. The process of instantiation would correspond to the hiring of a new assistant cook straight out of cooking school. Since many people are not sure about the exact meaning of the word object, I will try to avoid referring to objects in class and instead exclusively use the terms class and instance in the meanings defined above. Summary Classes are templates for meaningful units of the program you are writing. You can instantiate a class one or many times, which means loading it into memory and start using it. Each time you do that, you get an instance. Classes have methods, which are sets of instructions to perform certain tasks, and variables which are places for the temporary storage of information.
Classes interact by sending messages to each other. Most such messages are transmitted in the form of method calls, which is a concept similar to that of subroutine calls of other programming languages, but is more flexible and powerful.