System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə5/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   2   3   4   5   6   7   8   9   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

Java: A Beginner’s Guide is your gateway to the Herb Schildt series of Java
programming books. Here are some others that you will find of interest:
Java: The Complete Reference
Herb Schildt’s Java Programming Cookbook
The Art of Java
Swing: A Beginner’s Guide
Introducing JavaFX 8 Programming


Chapter 1
Java Fundamentals
Key Skills & Concepts

Know the history and philosophy of Java

Understand Java’s contribution to the Internet

Understand the importance of bytecode

Know the Java buzzwords

Understand the foundational principles of object­oriented programming
y
istory
opics
utorials
ffers & Deals
ighlights
ettings
Support
Sign Out


I

Create, compile, and run a simple Java program

Use variables

Use the if and for control statements

Create blocks of code

Understand how statements are positioned, indented, and terminated

Know the Java keywords

Understand the rules for Java identifiers
n computing, few technologies have had the impact of Java. Its creation in the early
days of the Web helped shape the modern form of the Internet, including both the
client and server sides. Its innovative features advanced the art and science of
programming, setting a new standard in computer language design. The forward­
thinking culture that grew up around Java ensured it would remain vibrant and alive,
adapting to the often rapid and varied changes in the computing landscape. Simply put:
not only is Java one of the world’s most important computer languages, it is a force that
revolutionized programming and, in the process, changed the world.
Although Java is a language often associated with Internet programming, it is by no
means limited in that regard. Java is a powerful, full­featured, general­purpose
programming language. Thus, if you are new to programming, Java is an excellent
language to learn. Moreover, to be a professional programmer today implies the ability
to program in Java—it is that important. In the course of this book, you will learn the
basic skills that will help you master it.
The purpose of this chapter is to introduce you to Java, beginning with its history, its
design philosophy, and several of its most important features. By far, the hardest thing
about learning a programming language is the fact that no element exists in isolation.
Instead, the components of the language work in conjunction with each other. This
interrelatedness is especially pronounced in Java. In fact, it is difficult to discuss one
aspect of Java without involving others. To help overcome this problem, this chapter
provides a brief overview of several Java features, including the general form of a Java
program, some basic control structures, and simple operators. It does not go into too
many details, but, rather, concentrates on general concepts common to any Java
program.
THE HISTORY AND PHILOSOPHY OF JAVA


THE HISTORY AND PHILOSOPHY OF JAVA
Before one can fully appreciate the unique aspects of Java, it is necessary to understand
the forces that drove its creation, the programming philosophy that it embodies, and
key concepts of its design. As you advance through this book, you will see that many
aspects of Java are either a direct or indirect result of the historical forces that shaped
the language. Thus, it is fitting that we begin our examination of Java by exploring how
Java relates to the larger programming universe.
The Origins of Java
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems in 1991. This language was initially called “Oak”
but was renamed “Java” in 1995. Somewhat surprisingly, the original impetus for Java
was not the Internet! Instead, the primary motivation was the need for a platform­
independent language that could be used to create software to be embedded in various
consumer electronic devices, such as toasters, microwave ovens, and remote controls.
As you can probably guess, many different types of CPUs are used as controllers. The
trouble was that (at the time) most computer languages were designed to be compiled
into machine code that was targeted for a specific type of CPU. For example, consider
the C++ language.
Although it was possible to compile a C++ program for just about any type of CPU, to
do so required a full C++ compiler targeted for that CPU. The problem, however, is that
compilers are expensive and time consuming to create. In an attempt to find a better
solution, Gosling and the others worked on a portable, cross­platform language that
could produce code that would run on a variety of CPUs under differing environments.
This effort ultimately led to the creation of Java.
About the time that the details of Java were being worked out, a second, and ultimately
more important, factor emerged that would play a crucial role in the future of Java.
This second force was, of course, the World Wide Web. Had the Web not taken shape at
about the same time that Java was being implemented, Java might have remained a
useful but obscure language for programming consumer electronics. However, with the
emergence of the Web, Java was propelled to the forefront of computer language
design, because the Web, too, demanded portable programs.
Most programmers learn early in their careers that portable programs are as elusive as
they are desirable. While the quest for a way to create efficient, portable (platform­
independent) programs is nearly as old as the discipline of programming itself, it had


taken a back seat to other, more pressing problems. However, with the advent of the
Internet and the Web, the old problem of portability returned with a vengeance. After
all, the Internet consisted of a diverse, distributed universe populated with many types
of computers, operating systems, and CPUs.
What was once an irritating but low­priority problem had become a high­profile
necessity. By 1993 it became obvious to members of the Java design team that the
problems of portability frequently encountered when creating code for embedded
controllers are also found when attempting to create code for the Internet. This
realization caused the focus of Java to switch from consumer electronics to Internet
programming. So, although it was the desire for an architecture­neutral programming
language that provided the initial spark, it was the Internet that ultimately led to Java’s
large­scale success.
Java’s Lineage: C and C++
The history of computer languages is not one of isolated events. Rather, it is a
continuum in which each new language is influenced in one way or another by what has
come before. In this regard, Java is no exception. Before moving on, it is useful to
understand where Java fits into the family tree of computer languages.
The two languages that form Java’s closest ancestors are C and C++. As you may know,
C and C++ are among the most important computer languages ever invented and are
still in widespread use today. From C, Java inherits its syntax. Java’s object model is
adapted from C++. Java’s relationship to C and C++ is important for a number of
reasons. First, at the time of Java’s creation, many programmers were familiar with the
C/C++ syntax. Because Java uses a similar syntax, it was relatively easy for a C/C++
programmer to learn Java. This made it possible for Java to be readily utilized by the
pool of existing programmers, thus facilitating Java’s acceptance by the programming
community.
Second, Java’s designers did not “reinvent the wheel.” Instead, they further refined an
already highly successful programming paradigm. The modern age of programming
began with C. It moved to C++ and then to Java. By inheriting and building on that rich
heritage, Java provides a powerful, logically consistent programming environment that
takes the best of the past and adds new features related to the online environment and
advances in the art of programming. Perhaps most important, because of their
similarities, C, C++, and Java define a common, conceptual framework for the
professional programmer. Programmers do not face major rifts when switching from
one language to another.


Java has another attribute in common with C and C++: it was designed, tested, and
refined by real working programmers. It is a language grounded in the needs and
experiences of the people who devised it. There is no better way to produce a top­flight
professional programming language.
One last point: although C++ and Java are related, especially in their support for
object­oriented programming, Java is not simply the “Internet version of C++.” Java
has significant practical and philosophical differences from C++. Furthermore, Java is
not an enhanced version of C++. For example, it is neither upwardly nor downwardly
compatible with C++. Moreover, Java was not designed to replace C++. Java was
designed to solve a certain set of problems. C++ was designed to solve a different set of
problems. They will coexist for many years to come.
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. First, the creation of Java simplified Internet
programming in general, acting as a catalyst that drew legions of programmers to the
Web. Second, Java innovated a new type of networked program called the applet that
changed the way the online world thought about content. Finally, and perhaps most
importantly, Java addressed some of the thorniest issues associated with the Internet:
portability and security.
Java Simplified Web-Based Programming
From the start, Java simplified web­based programming in a number of ways. Arguably
the most important is found in its ability to create portable, cross­platform programs.
Of nearly equal importance is Java’s support for networking. Its library of ready­to­use
functionality enabled programmers to easily write programs that accessed or made use
of the Internet. It also provided mechanisms that enabled programs to be readily
delivered over the Internet. Although the details are beyond the scope of this book, it is
sufficient to know that Java’s support for networking was a key factor in its rapid rise.
Java Applets
At the time of Java’s creation, one of its most exciting features was the applet. 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
automatically. Applets were intended to be small programs, typically used to display


data provided by the server, handle user input, or provide simple functions, such as a
loan calculator. The key feature of applets is that they execute locally, rather than on
the server. In essence, the applet allowed some functionality to be moved from the
server to the client.
The creation of the applet was important because, at the time, it expanded the universe
of objects that could move about freely in cyberspace. In general, there are two very
broad categories of objects that are transmitted between the server and the client:
passive information and dynamic active programs. For example, when you read your e­
mail, you are viewing passive data. Even when you download a program, the program’s
code is still only passive data until you execute it. By contrast, the applet is a dynamic,
self­executing program. Such a program is an active agent on the client computer, yet it
is delivered by the server.
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, and for reasons that will be explained shortly, JDK 9 began their phase­out
process. Finally, applet support was removed by JDK 11.
Ask the Expert
Q
: What is C# and how does it relate to Java?
A
: A few years after the creation of Java, Microsoft developed the C# language.
This is important because C# is closely related to Java. In fact, many of C#’s
features directly parallel Java. Both Java and C# share the same general C++­style
syntax, support distributed programming, and utilize a similar object model. There
are, of course, differences between Java and C#, but the overall “look and feel” of
these languages is very similar. This means that if you already know C#, then
learning Java will be especially easy. Conversely, if C# is in your future, then your
knowledge of Java will come in handy.
Security
As desirable as dynamic, networked programs are, they also present serious problems


in the areas of security and portability. Obviously, 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. As you will see, Java addressed these problems in an effective and elegant way.
Let’s look a bit more closely at each, beginning with security.
As you are likely aware, every time that you download a program, you are taking a risk
because the code you are downloading might contain a virus, Trojan horse, or other
harmful code. At the core of the problem is the fact that malicious code can cause
damage because it has gained unauthorized access to system resources. For example, a
virus program might gather private information, such as credit card numbers, bank
account balances, and passwords, by searching the contents of your computer’s local
file system. In order for Java to enable programs to be safely downloaded and executed
on the client computer, it was necessary to prevent them from launching such an attack.
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. (You
will see how this is accomplished shortly.) The ability to download an application with a
high level of confidence that no harm will be done contributed significantly to Java’s
early success.
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 types of systems. In other words, a
mechanism that allows the same application to be downloaded and executed by a wide
variety of CPUs, operating systems, and browsers is required. It is not practical to have
different versions of the same application for different computers. The same
application code must work in all computers. Therefore, some means of generating
portable code was needed. As you will soon see, the same mechanism that helps ensure
security also helps create portability.
Java’s Magic: The Bytecode
The key that allowed Java to address both the security and the portability problems just
described is that the output of a Java compiler is not executable code. Rather, it is
bytecode. Bytecode is a highly optimized set of instructions designed to be executed by
what is called the Java Virtual Machine (JVM), which is part of the Java Runtime


Environment (JRE). In essence, the original JVM was designed as an interpreter for

Yüklə 83 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə