1_1_java_intro
1_1_java_intro
- To fully understand Java, one must understand the reasons behind its creation, the forces
that shaped it, and the legacy that it inherits
- Java is a blend of the best elements of its rich heritage combined with the innovative
concepts required by its unique mission
- Java is first and foremost a programming language.
- Computer language innovation and development occur for two fundamental reasons:
- To adapt to changing environments and uses
- To implement refinements and improvements in the art of programming
Java’s Lineage
- Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems, Inc. in 1991
- It took 18 months to develop the first working version.
- This language was initially called “Oak,” but was renamed “Java” in 1995
- the original impetus for Java was not the Internet!
- primary motivation was the need for a platform-independent (that is, architecture-neutral)
language
- that could be used to create software to be embedded in various consumer electronic
devices, such as microwave ovens and remote controls
How Java Impacted the Internet
- The Internet helped catapult Java to the forefront of programming, and Java, in turn, had
a profound effect on the Internet.
- In addition to simplifying web programming in general, Java innovated a new type of
networked program called the applet that changed the way the online world thought
about content.
- Java also addressed some of the thorniest issues associated with the Internet: portability
and security
Java Applets
- An applet is a special kind of Java program that is designed to be transmitted over the
Internet and automatically executed inside a Java-compatible web browser
- If the user clicks a link that contains an applet, the applet will download and run in the
browser
- Applets were intended to be small programs
- the applet allowed some functionality to be moved from the server to the client
- In the early days of Java, applets were a crucial part of Java programming.
- They illustrated the power and benefits of Java, added an exciting dimension to web
pages, and enabled programmers to explore the full extent of what was possible with
Java.
- Although it is likely that there are still applets in use today, over time they became less
important
Security
- a program that downloads and executes on the client computer must be prevented from
doing harm
- It must also be able to run in a variety of different environments and under different
operating systems
- malicious code can cause its damage because it has gained unauthorized access to
system resources
- Java achieved this protection by enabling you to confine an application to the Java
execution environment and prevent it from accessing other parts of the computer
Portability
- Portability is a major aspect of the Internet because there are many different types of
computers and operating systems connected to it
- If a Java program were to be run on virtually any computer connected to the Internet,
there needed to be some way to enable that program to execute on different systems
- The same application code must work on all computers
Moving Beyond Applets
- applets rely on a Java browser plug-in. Thus, for an applet to work, the browser must
support it
- beginning with JDK 9, the phase-out of applets was begun, with support for applets being
deprecated
- a few years after Java’s creation an alternative to applets was added to Java.
- Called Java Web Start, it enabled an application to be dynamically downloaded from a
web page
- It was a deployment mechanism that was especially useful for larger Java applications
that were not appropriate for applets
- The difference between an applet and a Web Start application is that a Web Start
application runs on its own, not inside the browser [removed from jdk 11]
- jlink tool added by JDK 9. It can create a complete run-time image that includes all
necessary support for your program, including the JRE.
- Another part is the jpackage tool. Added by JDK 16, it can be used to create a
ready-to-install application
The Java Buzzwords
- Simple
- Secure
- Portable
- Object-oriented
- Robust
- Multithreaded
- Architecture-neutral
- Interpreted
- High performance
- Distributed
- Dynamic
Java’s Magic: The Bytecode
- a program can be conceptually organized around its code or around its data
- That is, some programs are written around “what is happening” and others are written
around “who is being affected.”
- First approach characterizes a program as a series of linear steps (that is, code)
- can be thought of as code acting on data
- Procedural languages such as C employ this model to considerable success
- problems with this approach appear as programs grow larger and more complex
- Object-oriented programming, was conceived to manage increased complexity
- OOP organizes a program around its data (that is, objects) and a set of well-defined
interfaces to that data
- An OO Program can be characterized as data controlling access to code
Object Oriented Programming (Abstraction)
- Encapsulation is the mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse
- One way to think about encapsulation is as a protective wrapper that prevents the code
and data from being arbitrarily accessed by other code defined outside the wrapper
- Access to the code and data inside the wrapper is tightly controlled through a
well-defined interface
- To relate this to the real world, consider the automatic transmission on an automobile.
- It encapsulates hundreds of bits of information about your engine, such as how much you
are accelerating, the pitch of the surface you are on, and the position of the shift lever
- You, as the user, have only one method of affecting this complex encapsulation: by
moving the gear-shift lever.
- You can’t affect the transmission by using the turn signal or windshield wipers, for
example
- Thus, the gear-shift lever is a well-defined (indeed, unique) interface to the transmission
Encapsulation …
- Further, what occurs inside the transmission does not affect objects outside the
transmission.
- For example, shifting gears does not turn on the headlights! Because an automatic
transmission is encapsulated, dozens of car manufacturers can implement one in any
way they please
- However, from the driver’s point of view, they all work the same. This same idea can be
applied to programming.
- The power of encapsulated code is that everyone knows how to access it and thus can
use it regardless of the implementation details—and without fear of unexpected side
effects
- In Java, the basis of encapsulation is the class
- A class defines the structure and behavior (data and code) that will be shared by a set of
objects.
- Each object of a given class contains the structure and behavior defined by the class, as
if it were stamped out by a mold in the shape of the class. For this reason, objects are
Encapsulation …
- Each object of a given class contains the structure and behavior defined by the class, as
if it were stamped out by a mold in the shape of the class.
- For this reason, objects are sometimes referred to as instances of a class.
- Thus, a class is a logical construct; an object has physical reality.
- When you create a class, you will specify the code and data that constitute that class.
- Collectively, these elements are called members of the class
- Specifically, the data defined by the class are referred to as member variables or instance
variables
- The code that operates on that data is referred to as member methods or just methods
- In properly written Java programs, the methods define how the member variables can be
used.
- This means that the behavior and interface of a class are defined by the methods that
operate on its instance data
Encapsulation …
- Since the purpose of a class is to encapsulate complexity, there are mechanisms for
hiding the complexity of the implementation inside the class.
- Each method or variable in a class may be marked private or public.
- The public interface of a class represents everything that external users of the class need
to know, or may know.
- The private methods and data can only be accessed by code that is a member of the
class.
- Therefore, any other code that is not a member of the class cannot access a private
method or variable.
- Since the private members of a class may only be accessed by other parts of your
program through the class’ public methods, you can ensure that no improper actions take
place.
- Of course, this means that the public interface should be carefully designed not to expose
too much of the inner workings of a class
Public methods can be used to protect private data
Inheritance
- Inheritance is the process by which one object acquires the properties of another object.
- This is important because it supports the concept of hierarchical classification.
- As mentioned earlier, most knowledge is made manageable by hierarchical (that is,
top-down) classifications.
- For example, a Golden Retriever is part of the classification dog, which in turn is part of
the mammal class, which is under the larger class animal.
- Without the use of hierarchies, each object would need to define all of its characteristics
explicitly.
- However, by use of inheritance, an object need only define those qualities that make it
unique within its class.
- It can inherit its general attributes from its parent.
- Thus, it is the inheritance mechanism that makes it possible for one object to be a
specific instance of a more general case
Inheritance
- Most people naturally view the world as made up of objects that are related to each other
in a hierarchical way, such as animals, mammals, and dogs.
- If you wanted to describe animals in an abstract way, you would say they have some
attributes, such as size, intelligence, and type of skeletal system.
- Animals also have certain behavioral aspects; they eat, breathe, and sleep.
- This description of attributes and behavior is the class definition for animals.
- If you wanted to describe a more specific class of animals, such as mammals, they would
have more specific attributes, such as type of teeth and mammary glands.
- This is known as a subclass of animals, where animals are referred to as mammals’
superclass.
- Since mammals are simply more precisely specified animals, they inherit all of the
attributes from animals.
- A deeply inherited subclass inherits all of the attributes from each of its ancestors in the
class hierarchy
Inheritance
Inheritance
Polymorphism
- Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface
to be used for a general class of actions.
- The specific action is determined by the exact nature of the situation.
- Consider a stack (which is a last-in, first-out list).
- You might have a program that requires three types of stacks.
- One stack is used for integer values, one for floating-point values, and one for characters.
- The algorithm that implements each stack is the same, even though the data being
stored differs.
- In a non–object-oriented language, you would be required to create three different sets of
stack routines, with each set using different names.
- However, because of polymorphism, in Java you can specify a general set of stack
routines that all share the same names
Polymorphism
- More generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods.”
- This means that it is possible to design a generic interface to a group of related activities.
- This helps reduce complexity by allowing the same interface to be used to specify a
general class of action.
- It is the compiler’s job to select the specific action (that is, method) as it applies to each
situation.
- You, the programmer, do not need to make this selection manually.
- You need only remember and utilize the general interface.
- Extending the dog analogy, a dog’s sense of smell is polymorphic.
- If the dog smells a cat, it will bark and run after it.
- If the dog smells its food, it will salivate and run to its bowl.
- The same sense of smell is at work in both situations
- This same general concept can be implemented in Java as it applies to methods within a
Java program