1 / 88

Unit 1: Java Introduction and Eclipse

Unit 1: Java Introduction and Eclipse. Kirk Scott. 1.1 Java 1.2 Object-Orientation 1.3 UML 1.4 The Eclipse Development Environment. 1.1 Java. Java is an object-oriented programming language. It is important to us because Android programming uses Java.

haig
Download Presentation

Unit 1: Java Introduction and Eclipse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 1: Java Introduction and Eclipse Kirk Scott

  2. 1.1 Java • 1.2 Object-Orientation • 1.3 UML • 1.4 The Eclipse Development Environment

  3. 1.1 Java

  4. Java is an object-oriented programming language. • It is important to us because Android programming uses Java. • However, Java is much more than just the language for Android.

  5. What are some of Java’s characteristics that distinguish it from other programming languages? • What features does it have that might make it useful in situations where other languages would not be as suitable?

  6. From a technical point of view, Java has one fundamental characteristic which all other important characteristics stem from. • Java is a language which is interpreted on a simulated hardware architecture. It is possible to design hardware that runs Java code directly, but in common computing environments such as Windows and Unix, installation of Java on a machine means that the simulation has been installed.

  7. When programs run, they run on the simulation. For other languages such as C or C++, compilation results in an executable target file which consists of machine language for the architecture for which the compiler was designed. • For Java, the process is still referred to as compilation, but it is not literal compilation. When Java source code is “compiled” the result is “byte code”, which is code which runs on the simulated Java machine.

  8. In such a design, the programming language is merely a layer in a system’s software architecture and it is not dependent on the underlying hardware architecture of the machine. • As a result, in theory at least, Java is potentially a “universal” programming language. • Versions of Java exist for various hardware architectures. These implementations differ internally.

  9. However, the intent is for the Java code generated for these different environments to be exactly the same. • Thus, Java programs should be freely portable between them. • If a program is written in one environment, no changes in the source code should be necessary in order for it to be compiled or, if already compiled, to be directly run in a different environment.

  10. The statement made above is the ideal, which is encapsulated in the phrase “Write once, run everywhere”. • This applies to desktop type computers. • Even in this case, the reality is that subtle differences in implementations of the Java machine lead to problems which have been encapsulated in the phrase “Write once, debug everywhere”.

  11. For us, it’s important to realize that Java as used in Android does differ from Android as used in other environments. • The basic syntax is the same, but the overall structure of Android apps is different from the structure of apps in a desktop environment. • An Android app will only run on an Android device. It will not run elsewhere.

  12. As with all system and software design decisions, the nature of Java involves trade-offs. • One of the driving forces behind Java was originally the World Wide Web. • If a “universal” programming language exists, then programs can be downloaded and run on users’ machines regardless of the hardware involved. • Now Java has been adapted to accomplish something similar in the cell phone environment.

  13. The big trade-off in using Java is that Java programs are considerably slower than genuine compiled programs which run directly on the hardware of a given machine. • Interpreting Java code on a simulated machine may take in the range of ten times as long. • As hardware has become faster, and as the World Wide Web and cell phones have become the preferred means of distributing programs, this trade-off of speed for portability has been well worth it.

  14. Another one of the design decisions in Java was driven by this desire to download runnable programs. • Some languages, such as C and C++, give the programmer the ability to write code which directly accesses memory addresses in a machine’s hardware. • This is done by means of pointers. • This is not possible in Java. • It may seem odd that a modern, advanced language does not allow this. • It is possible to write code for linked structures in Java, but not code which relies on memory addresses.

  15. Programmers know that programs which directly access memory may do harmful things to a system, whether intentionally or unintentionally. • For this reason, direct memory access is undesirable in Java. • When users download programs, they want to be sure that these programs cannot do destructive things to their systems which result from direct memory access. • Thus, by design, Java supports both portability and safety of programs.

  16. The goal of this and the following units is to give an introduction to programming in the Java language and then progress to programming Android apps. • Java is object-oriented. • It is designed to support event-driven programming. • In other words, it can be used to create programs with graphical user interfaces which support point-and-click actions.

  17. Java has basic syntactic features found in structured programming languages, namely sequential execution of blocks of code, conditional execution (if statements), and iterative execution (loops). • These syntactical elements will be covered in following units, as well as descriptions of the object-oriented concepts which support more advanced programming.

  18. Experts may argue about the relative technical merits of Java. • It must be noted at the outset that it has certain disadvantages for the learner. • Java was not designed for teaching or learning purposes. • It was designed for building complex applications. • In addition to many advanced features within the language itself, it comes with libraries of complete code components which an experienced programmer can use instead of writing components from scratch.

  19. Neither the complete syntax nor the libraries can be learned in a single course. • Much of what is learned at the outset may not seem particularly useful. • However, it is necessary to cover the foundations of object-orientation before it becomes possible to write more interesting Java applications or Android apps.

  20. Section Summary • The Java language is interpreted on a simulated machine architecture. • This makes Java source and target code highly portable. • This makes it possible to download Java code from the World Wide Web, although Java programs may not run especially quickly. • In order to make downloaded programs safe, Java programs do not support direct memory access.

  21. Java is an object-oriented language containing the features needed in order to write graphical applications with point-and-click interfaces. • Java was designed as a production language, not a teaching language. It contains many features and comes with many libraries. • It is possible to do things in Java that cannot be done, or can only be done with difficulty in other languages.

  22. Vocabulary • Interpreted languages, compiled languages. • Source code, target code. • Pointers, direct memory access. • Object-orientation, software applications. • Event-driven programming. • Point-and-click, graphical user interfaces. • Sequential execution. • Conditional execution (if statements). • Iterative execution (loops).

  23. 1.2 Object-Orientation

  24. Programming languages and paradigms have evolved progressively over time. • Some of the things that have driven change include: • New hardware, which has the capability of supporting new programming paradigms; • new problems to be solved, which require new programming capabilities; • and limitations in existing systems which are exposed when large or complex projects are developed.

  25. Object-oriented programming is one of the more recent programming paradigms. • Its development and characteristics reflect these three points. • It is a computationally intensive paradigm which could not have been supported by primitive machines; • the programming of graphical user interfaces is one area where its use is apparent; • and because it has solved at least some of the problems associated with structured programming, its most immediate predecessor, it has largely supplanted it for the development of complex software systems.

  26. Java is an object-oriented language. • There are many object-oriented languages, and each implements the paradigm in its own way. • It is not the intent of these notes to give a full explanation of object-orientation or all aspects of object-orientation in Java.

  27. However, it is desirable to start with some general ideas regarding object-orientation, building a vocabulary for the basic concepts involved. • A fuller understanding will arise as the implementation of these concepts in Java is examined, and as more advanced concepts and their implementation are added to the basics.

  28. Two fundamental terms are involved in object-orientation, and will be repeated over and over again. • The terms are: • Class and object. • A class is a model for objects. • You could also say that a class is a template, or pattern for objects. • To be more concrete, a class is a piece of computer code which gives the complete specifications for objects. • The class definition is part of a program’s source code.

  29. During the course of a program run it is possible to make a call which causes an object to be created according to the specifications of a given class. • An object can be referred to as an instance of this class. • A program may use more than one class, and more than one instance of any of these classes may be created during the course of a program run. • The work of the program consists of manipulating these objects.

  30. Two additional concepts are involved in object-orientation. • Computer programs include sequences of instructions which belong together and can be executed to do some specific thing. • In object-oriented programming, these are referred to as methods. • Methods are part of a class definition.

  31. Computer programs also include declarations of data, such as numbers, sequences of characters, etc., which are to be manipulated. • In their simplest form, these are variables. • Variables are symbolic names which can be used to refer to stored quantities or values in a program. • Different kinds of variables can be used in different places in a program. • The variables associated with classes and objects are called instance variables, and like methods, they are part of a class definition.

  32. Although both appear in the class definition, methods and instance variables are treated quite differently in the object-oriented paradigm. • In a program there may be more than one instance of more than one class. • The following distinction between methods and instance variables applies: • There is no need at run time for each instance of a class to get its own copy of method code.

  33. Methods are defined in the class and are shared by all objects of that class that are in existence in a program at a given time. • Thus, the methods for all objects of a class are exactly the same. • However, when objects of a class are created, they get their own copies of the instance variables. • Each object of a class gets the same set of variables, but different objects can have different values for the variables.

  34. So, what’s the relationship between methods and instance variables? • A fundamental aspect of the object-oriented paradigm is referred to as encapsulation. • Each object has its own copies of the instance variables. • The values of these variables can be accessed or changed during the course of a program run in only one way: • By calling a method which has the purpose of revealing or changing the value of the instance variable.

  35. This enforces a strong discipline on programmers. • It is not possible at random points in program code to simply change instance variable values in a way that may seem convenient at the moment. • Changes can only be made within the framework of encapsulation. • Undisciplined changing of data values was considered one of the weaknesses of structured programming. • This is corrected in object-oriented programming.

  36. Methods Instance variables Figure 1. Instance variables and methods. • Here is a simple graphical representation of an object, showing the instance variables it contains and the methods it uses.

  37. A handy way of remembering the meaning of the figure is by thinking of the object as a fried egg. • Once the object is created, a program only has access to the yolk through the white. • It is important to understand that the programmer making use of the object needs to know what methods are available for manipulating it. • However, it is not necessary to know how the method code works.

  38. Although the existence of some instance variables may be clear, the programmer does not need to know how they are implemented, and does not need to know all of the instance variables in order to make use of the object. • In this sense, the object can be though of as a black box. • The programmer can do certain things to it, with certain results expected, but how these results are accomplished is hidden. • The methods provide an interface to the instance variables and their values contained inside the object.

  39. Section Summary • Object-orientation relies on advanced hardware capabilities, solves problems that could not be solved with earlier programming paradigms, and addresses some of the shortcomings of previous paradigms. • Java implements the object-oriented paradigm, and basic object-oriented terms and ideas are helpful in explaining Java. • Classes and objects are two of the underlying concepts of object-orientation.

  40. Methods and variables are two additional key concepts of object-orientation. • When objects are created from a class definition, they share the code for methods, but they each get their own copies of the instance variables. • Objects encapsulate their instance variables. The values of the variables can only be accessed or changed if there is a method in the class that permits it. • A programmer using an object only has access through the methods, and does not need to know how the methods work or all of the instance variables there might be.

  41. Vocabulary • Paradigm. • Computationally intensive. • Classes, objects. • Methods, instance variables. • Encapsulation.

  42. 1.3 UML

  43. Depending on the source, the acronym UML is said to stand for “unified modeling language” or “universal modeling language”. • In any case, it is a set of conventions that can be used to diagram object-oriented software. • It is possible to pictorially represent things like classes and their methods and instance variables; • objects of those classes; relationships between classes; • and so on.

  44. These notes will not give complete coverage of UML. • However, at various points in the presentation of Java it may be helpful to represent an idea or relationship visually. • In those cases, the corresponding UML notation will be introduced and used for that purpose.

  45. UML has a vocabulary as well as a set of visual symbols which are used to represent object-oriented components. • The terms “class” and “object” have already been used in these notes. • These terms are used to mean the same thing in UML.

  46. The terms “instance variable” and “method” have already been introduced. • The corresponding terms in UML are “attribute” and “operation”. • There is more than one object-oriented language. • UML is neutral. • An object-oriented program in any of these languages could be described using UML terminology.

  47. Since this book only covers Java, the terms “instance variable” and “method” will continue to be used. • It should be noted that in the official Java documentation, the term “field” is used for “instance variable”. • Since this is the term chosen by the language designers, in some sense it may be more technically correct. • However, the term “field” may be confusing because it has other uses in various programming environments. “Instance variable” will be used instead because it is more descriptive.

  48. In order to illustrate UML, it is necessary to have an example. • It is possible to introduce such an example and talk about object-oriented ideas without even knowing any Java. • The example which will be continued throughout the notes is based on the following idea: • It is possible to store seeds in a cup.

  49. This may seem a little impractical. • You typically think of a cup as containing a liquid, and it’s not clear what kinds of interesting programs you might write involving cups and seeds. • Any introductory example of a class would tend to be impractical anyway. • The cup and seed example is given because it is a component of a game program which is developed later on in the notes.

  50. Classes and objects are represented using rectangles in UML. • Relationships are represented using labels, lines, arrowheads, and other symbols. • In its simplest form, a class can be represented by a rectangle containing the name of the class. • The name is capitalized and given in bold face. • For example, this would represent the Cup class:

More Related