Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
BIT 1204/CSC 1101 INTRODUCTION TO PROGRAMMING IN JAVA: THE JAVA PROGRAMMING LANGUAGE LESSON 2 CONTENTS 1. Object oriented programming languages 2. Origins of Java 2.1. "Java enabled" 2.2. Programming in Java 3. The mechanics of creating an executable Java "application" program 3.1. Source code 3.2. Compilation 3.3. Summary 4. Program execution 5. Application Programming Interface (API) 6. Java Environments 1. OBJECT ORIENTED PROGRAMMING LANGUAGES Java is an object oriented language, i.e. it follows the object-oriented paradigm. Object oriented programming is currently extremely popular although the concept has been around since the early 1970s. A brief overview of well known object oriented languages other than Java is given below (listed in chronological order): SMALLTALK'80. The archetypal objectoriented language/environment. Everything in Smalltalk is an object. SIMULA. The first language to introduce the concept of objects. Essentially ALGOL'60 with objects. (Not usually considered to be a "real" OO language but significant within the context given here). Eiffel. Designed in 1980s as a simpler and more efficient implementation of the object-oriented paradigm than that espoused by Smalltalk. C++. Essentially C with object-oriented features. Best be described as a hybrid between a conventional imperative language and an object-oriented programming language. C#. New OO lnaguage based on C++ and Java. There are many more, but the above are the most significant. 2. ORIGINS OF JAVA The Java language started life in 1990 when a team at Sun, headed by James Gosling, designed a new programming language, known as Oak, for the development of consumer electronics software (the Green Project). It was not till the introduction of the WWW that the language Java came into being. With the introduction of the WWW the Oak development team quickly realised that Oak style programs could be invoked over the Internet using what has become known as a Java Applet. The significance of this is that the language would be completely "platform" independent ---remember that previously to run (say) a C program on a PC platform you needed a PC compiler and to run the same program on a UNIX platform you needed a UNIX compiler, Etc. To demonstrate this Sun developed the worlds first Java enabled Web browser and called it HotJava. One way of running Java is to use the Java 2 SDK --- Software Development Kit (previously known, with respect to Java Version 1.1, as the JDK --- Java Development Kit). There are three editions of Version 1.4 of the Java 2 Platform and the Java 2 SDK: 1. J2SE (Java 2 Standard Edition): The edition we will be using. 2. J2EE (Java 2 Enterprise Edition): An extended version of the J2SE which has additional facilities to enable Java to be used with distributed systems. 3. J2ME (Java 2 Micro Edition): A slimmed down version of the J2SE which enables Java to be used in embedded systems such as smart cards, pagers, mobile phones, etc. 2.1. Java "Enabled" This platform independence sets Java apart from any other language. However, Java programs do not have to be run from a Java enabled WWW browser, they can be run independently as applications in the traditional manner. The term Java enabled means that inside the browser there is a hypothetical machine known as a Java Virtual Machine (JVM) which can run Java for that particular computer. The Java that comes over the net when an Applet is invoked is encoded in something known as Java Byte As a relatively young language (compared Code (JBC). A JVM resident on a particular to languages such as Ada or C) Java is still computer can interpret JBC and consequently evolving. The core of the language is small "run" the Applet on that computer. (with respect to other "OO" languages such as C++), however more and more packages (libraries) are constantly being added. 2.2. Programming in Java Version 1.0 of the language was launched, by Sun, in 1995. Version 1.1 was produced From Section 2 we have seen that we can write in 1997, and included minor modifications two different kinds of Java program: to the core language and major additions to the libraries. Version 1.2 came out in 1. Applications: Stand alone programs December 1998 and included many significant changes that resulted in this version being referred to as "Java 2" and then as the "Java 2 Platform". Version 1.2 was followed, in 2000, by version 1.3. The current version (summer 2003) is 1.4, and this is the version we will be using. similar in nature to the kinds of programs that might be written in any other high level language (C, C++, Ada, etc.) 2. Applets (small applications): Programs that are executed from a Web browser such as Netscape Communicator. Applets typically allow the user to control execution through a Graphical User Interface (GUI) --- a collection of components such as buttons and scroll bars within a window. An Applet can be executed from any "Java enabled" browser. We will be focusing on the first of these approaches. 3. THE MECHANICS OF CREATING AN EXECUTABLE JAVA "APPLICATION" PROGRAM 3.1. Source Code As noted above JBC comprises a set of instructions written for a hypothetical machine known as the Java Virtual Machine (JVM). In this manner the Java language achieves its platform independence. We say that Java code is portable in that a Java program will run (without modification) on many types of machine. Portability is an important issue in software development and an often quoted advantage of the Java high level languages. Thus, given a Java source code file called (say) Mypro.java the Java compiler is invoked as follows: Using a high level language, such as Java, a program is written as ordinary text stored in a file. This text is referred to as the source code. A file containing text is usually referred to as a text file. Text files are created using a program called a text editor. This is a program the same as any other, input is usually from the keyboard and the output is a text file. Text editors allow users to create and edit text files. There are many examples of such programs (PFE, javac Mypro.java KWRITE), the more sophisticated are referred to as word processors and are This will produce a Java byte code file called designed to produce documents rather than Mypro.class. programs. Text editors can be viewed as simple word processing programs. It is good practice to give meaningful 3.3. Summary suffixes to source code files, in some cases (and this includes Java) this is a requirement To summarise the above, the stages in the of the "compiler" (see below). Thus Java production of a working Java program are as source code text files must have the suffix similarly C source code would have follows: the suffix .c and Ada source code the suffix 1. Create the program text (source code) .ada (although this is not a requirement of using a text editor. either C or Ada compilers). A plain text file, 2. Compile the source code to create an containing (say) some notes, might then executable file (the load module). have the suffix .txt. In this manner you can identify the different files contained in a This process is illustrated in Figure 1. Note that directory at a glance. in the figure the Java compiler also has input from packages, these are files containing preprogrammed Java classes which come with the Java language and which facilitate common 3.2. Compilation operations such as input and output. Because Once a program exists as a text file the next these operations are so common there seems stage is to compile (translate) this text into little point in writing them over and over again for every Java program that we write, and an executable form. Generally most compilers (e.g. C, C++ and Ada compilers) therefore they are provided as part of the translate source code into machine code. As language. noted previously the disadvantage of this is that different compilers will be required for (The "packages concept" is common to many different languages and machines (machine programming languages.) code is machine specific), i.e. languages such as C and Ada are not platform independent. Java, on the other hand, is platform independent because it compiles the source code into Java Byte Code (JBC). .java; Figure 1: The mechanics of creating an executable Java program. 4. PROGRAM EXECUTION In the case of languages such as C, C++ and Ada, once code has been compiled the result may be run (executed) through a command to the operating system comprising the name of the program (without any suffixes). However, in the case of Java --- to achieve the desired platform independence --- the Java byte code must be interpreted using an interpreter. Assuming the existence of a Java Byte Code (JBC) file called Mypro.class this can be achieved as follows: java Mypro Java can thus be viewed as a cross between a compiled high level language and an interpreted one. Note: Remeber that the alternative way of invoking a Java program is by writing it in the form of an Applet and invoking it from a (Java enabled) WWW page --- however this is not of interest to us at present. 5. APPLICATION PROGRAMMING INTERFACE (API) In Sub-section 3.3 we noted the existence and usage of the "packages" concept. Many programming languages provide a set of packages to support common operations such as Input and Output (I/O). Collectively the preprogrammed classes contained in the Java packages which are provided with the language are called the Application Programming Interface or API (a term also common to many programming language and not just Java). The Java API consists of a large collection of classes (hundreds) organised into different package (each package thus contains a number of classes). Currently (April 2005) the Java API comprises some 30 packages (some of which comprise several sub-packages). net --- for networking. applet --- for creating applets. i.e.java programs that tun on the WWW. We will only be considering the first three of these. We can inspect individual packages and their constants through Sun's Java WWW page at http://java.sun.com. If you go to this page and select "API Specifications" (listed near the top on the menu on the left under "reference") this will take you to another page ( http://java.sun.com/reference/api/index.html). If, under "Standard Edition", you now select "J2SE 1.5.0" this takes you to an extremely useful page ( http://java.sun.com/j2se/1.5.0/ docs/api/) shown in Figure 2. It is a good idea to add this URL to your bookmarks/ favourites. All the packages currently available are listed here (and all the classes). If you select a package this will bring up a new page listing all the classes in that package. By selecting an individual class you can then inspect the features of that class, and A package can therefore best be described as a collection of classes so on. For example if we select the package java.lang (which provides classes that are fundamental to the Java which logically fit together. The most commonly used packages are: programming language) and then "scroll" down the class summary and select the class system you will see a summary of all the fields and methods contained in lang --- for routine operations this class. (this package is always automatically compiled into every Java program). util --- for additional useful utilities. io --- for input and output (I/O). text --- for specialized formatting. awt --- for graphics and graphical user interfacing. Figure 2: The Java "J2SE 1.5.0 API specification" home page (Remeber that the java.lang package is always automatically incorporated into every Java program.) 6. JAVA INTEGRATED DEVELOPMENT ENVIRONMENTS There are a great many Java integrated development environments (IDEs) available (Sun's J2SE is not an environment). All these are available for PC platforms using Examples include: Windows 2000 or NT or later. There is also a version of Code Warrior that will run on 1. BlueJ: Developed as part of a Macintoshes. university research project about teaching object-orientation to An additional advantage offered by some of beginners and therefore probably these environments is that they can outperform well suited to COMP101. the J2SE in speed. A word of warning however, 2. Studio Standard 5: Sun's own Java most environments come with their own built-in IDE. Java compiler which may not be the latest 3. Microsoft's Visual J++. version (Java is upgraded by Sun at intervals) -4. JBuilder: Produced by Borland. - to upgrade such environments a new version 5. CodeWarrior: Produced by must be obtained --- at cost!. Metrowerks 6. JCreator. Created and maintained by Pius Nyaanga Momanyi. Last updated 27 April 2013