600 likes | 618 Views
Learn about the history of Java, its benefits, the Java environment, types of Java programs, and the basics of a Java application. Explore its platform independence, rich libraries, internationalization, distributed capabilities, security, threading, and object-oriented nature.
E N D
Introduction Elizabeth MacDonald The State University of New York at Albany
What we will cover tonight • History of Java • Benefits of Java • Java Environment • Types of Java programs • The Java Platforms • UML • Structured vs. Object Oriented Programming • Basics of a Java application
Code-Name Green • The name for Sun’s internal corporate research project aimed at providing intelligent consumer-electronic devices. • Result was Oak • C++ based language • Created by James Gosling • Named after an Oak tree outside his office • Name of new language changed to Java
Green in the red? • Green project didn’t go far • Marketplace for intelligent consumer-electronic devices was slow to rise • Sun didn’t win certain contracts • Was on the verge of being cancelled
Saved by the Web! • World Wide Web popularity explosion in 1993 saved project Green • Sun saw the potential for Java providing dynamic content to web pages • Java was formally announced in 1995 • Java is now used for large-scale enterprise applications, applications for consumer devices, and many other purposes.
“Write once run anywhere” • Platform independent and architecturally neutral • As opposed to C++, you do not need to recompile • This is due to the Java Virtual Machine (we will discuss that in a moment) • Bytecode can be ported across platforms with compatible versions of the JVM
Rich libraries • The term Java refers to both the language you can use to develop your own libraries, as well as the numerous existing classes that have already been developed. • Simplifies programming • Includes classes for building Graphical User Interfaces, file I/O, networking, web applications, etc.
Internationalization • Unlike most other languages, Java uses 16 bit Unicode characters • Can represent the phonetic and ideographic character sets of the entire world • C++ uses 8 bits ~ 256 different characters • 16 bit Java char ~ 65,535 different characters
Distributed • Java classes can execute remotely • Has full support for serialization into bytecode • Has networking capabilities built in • ADTs for distributed message passing
Security • Java has customizable Security settings • Java applets • Users would be afraid to run applets that could damage their machines • Prevent applets from doing any local file I/O or killing other running processes…
Threading • Java simplifies multithreading • A thread is a lightweight process • Multiple processes run through the same set of instructions in memory, as opposed to loading the application code into different memory locations • Java has encapsulated threads as an Abstract Data Type
Object Oriented • Java is a truly Object Oriented programming language • Everything has to reside in a class • Main method and all other methods have to be in a class • Has support for inheritance, polymorphism, message passing, …
To run a Java Program • Java instructions need to be translated into an intermediate language called bytecode • Then the bytecode is interpreted into a particular machine language
What is needed… • Compiler: A program that translates a program written in a high-level language into the equivalent machine language. • In the case of Java, this machine language is the bytecode. • Java Virtual Machine (JVM) - hypothetical computer developed to make Java programs machine independent
Java Virtual Machine • Java programs execute on the JVM. • The JVM is a virtual rather than a physical machine (although the JVM has been implemented in silicon.) • The JVM is typically implemented as a run-time interpreter • Translates Java bytecode instructions native instruction codes
Processing a Java Program • Source program: Written in a high-level language • Linker: Combines bytecode with other programs provided by the SDK and creates executable code • Loader: transfers executable code into main memory • Interpreter: reads and translates each bytecode instruction into machine language and then executes it
Java Runtime Environment • The java utility in the SDK provides the JVM as a run-time interpreter. • The JVM provides a run-time environment (JRE) • Enables programs to execute on a host platform. • The Java runtime can work with a security manager to determine which operations a program can perform on the host platform.
Program types • Java has four program types, although the differences among them are shallow. • The two most commonly used types are: • Application • Applet
Java Application • Standalone program in the sense of requiring only the JVM to execute. • Does not require a host program such as a browser. • It’s main method is used as its entry point • Has to have a method with the following signature… public static void main(String args[ ])
Java Applet • Small program typically downloaded from a server to a client machine. • JVM is built into the web browser • A Web browser acts as the host program for an applet. • An applet is typically launched through an HTML or equivalent document. • They are imbedded into HTML with the <applet> tag
Java Applet (cont’d) • An applet typically operates under a strict security manager, which imposes sandbox security. • This prevents an applet from performing potentially dangerous operations such as reading from or writing to the local disk. • An applet’s class descends from the standard Applet or JApplet class.
UML (part 1) • Unified Modeling Language • Meant to facilitate the design, development, deployment and maintenance of software systems. • Standardized in 1997 by the Object Management Group. • Combines methods from Object Modeling Technique (OMT) and Object-Oriented Software Engineering (OOSE). • UML diagrams can serve as high-level design specifications for software systems and, in this role, can guide code development.
UML (part 2) • Basic UML vocabulary consists of • Things: Entities to be modeled, whether concrete or abstract. • Relationships: Connections among things. • Diagrams: graphical depictions of things and their relationships • We will discuss the types of UML diagrams as they come up.
Structured Approach • Subset of procedural programming • Enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. • Frequently employs a top-down design model • Developers map out the overall program structure into separate subsections. • A defined function or set of similar functions is coded in a separate module or submodule, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. • After a module has been tested individually, it is then integrated with other modules into the overall program structure.
Object Oriented Approach • Provides a more natural and intuitive way to view the design process • Modeling software components just as we describe real-world objects • By their attributes, behaviors, and the messages they pass between each other • Objects • Reusable software components • Building blocks
Benefits of OO • Quality • Small, small-contained manageable objects reduces the complexity of the system development • Less error prone • Productivity • More reuse • Flexibility • Can add or modify objects while minimizing the impact on the rest of the system • This is achieved via encapsulation
Java and OO • Just because it’s written in Java, does not mean it is object-oriented • The standard Java libraries are not always good examples of OO programming. • Compromise between practical and efficiency considerations and truly good OO design
What is an object-oriented system? • Has the following 6 properties: • Abstraction with objects • Encapsulated classes • Communication via messages • Object lifetime • Class hierarchies • Polymorphism
Abstraction • A model of a real-world object or concept • Usually a simplified representation of a complex situation • Designing objects can be difficult • Have to decide on what attributes and behaviors are necessary • Address book example
Encapsulated Classes • Remember abstract data types? • The process of hiding all the internal details of an object from the outside world. • In Java, this is having definitions for attributes and methods inside a class definition. • State is changed using methods in the class.
Communication via Messages • Messages are how objects communicate with each other. • Follows client/server model. • Objects can send and receive messages. • Send = invoke a class method • Receive = have your method called by another object • Can also be invoked by the language runtime system or the operating system.
Class Hierarchies • An ordering of classes. • This includes: • Association • Aggregation • Composition • Inheritance
Teacher Student Association • A relationship between two classes. • Indicates how the objects of the classes relate to each other. • The relationship has some sort of name (verb). • Multiplicity specifies the number of objects that can participate in the association. * teaches learns from *
Car Dealership Car Aggregation • has-a relationship • The aggregate object includes other objects • The included objects are considered to be a part-of the aggregate *
Car Engine Composition • Special case of aggregation • The whole cannot exist without the aggregate object(s) 1
Inheritance • is-a relationship • A mechanism that allows one class (the subclass) to share the attributes and behaviors of another class (the superclass). • A subclass is a specialization of the generalized superclass. • The derived class may override or provide an alternate definition for an attribute or method of the superclass, or use the default behaviors of the superclass.
Animal Warm Blooded Cold Blooded Mammal Bird Flying Non-Flying Parrot Penguin Inheritance Example
More on Inheritance • In Java, all classes are implicitly derived from the Object class, making Object the root class. • Single inheritance is when a class is directly derived from only 1 parent class. • Multiple inheritance is when a class is derived from more than one parent classes. • Unlike other languages, Java does not support multiple inheritance.
Multiple Inheritance is BAD • Repeated Inheritance – a base can occur more than once in the parents list. • Ambiguity – a feature or method occurs more than once among the parents… which one is called? • This brings us to interfaces, the alternative to multiple inheritance.
Interfaces • In psychology, a “mask” that allows a person to behave in a manner appropriate to a particular role. • In Java, the specification of methods that a class using the interface must implement, or provide code for. • Defines the is-a relationship between classes. • You will NOT find code in an interface.
Polymorphism • Dynamic binding of an object to the appropriate method. (The actual binding of a call to a specific method is delayed until runtime.) Student +takeExam() : int GoodStudent BadStudent +takeExam() : int +takeExam() : int
Jumping into Java Programming • To create and run a Java program • Create a text file with a .java extension for the source code. For instance, source code for a first program might reside in the file Hi.java. • Write the source code. • Compile the source code into binary code. • Execute the binary code.