IntroJB01.pdf

(540 KB) Pobierz
Microsoft Word - CH01IntroductionToJavaAndJBuilder.DOC
Part I: Fundamentals of Programming
By now you have heard a lot about Java and are anxious to
start writing Java programs. The first part of the book is a
stepping stone that will prepare you to embark on the
journey of learning Java. You will begin to know Java and
will develop fundamental programming skills. Specifically,
you will learn how to write simple Java programs with
primitive data types, control statements, methods, and
arrays.
Chapter 1 Introduction to Java and JBuilder 4
Chapter 2 Primitive Data Types and Operations
Chapter 3 Control Statements
Chapter 4 Methods
Chapter 5 Arrays
1
1
Introduction to Java and JBuilder 4
Objectives
Learn about Java and its history.
Understand the relationship between Java and the
World Wide Web.
Become familiar with JBuilder 4.
Create Java projects, compile and run Java programs
with JBuilder 4.
Understand Java runtime environment.
Write a simple Java application.
Introduction
By now you have heard quite a lot about the exciting Java
programming language. It must seem as if Java is everywhere!
Your local bookstores are filled with Java books. There are
articles about Java in every major newspaper and magazine.
It is impossible to read a computer magazine without coming
across the magic word Java . You must be wondering why Java
is so hot. The answer is that it enables users to deploy
applications on the Internet. In fact, this is its main
distinguishing characteristic. The future of computing will
be profoundly influenced by the Internet, and Java promises
to remain a big part of that future. Java is the Internet
programming language.
You are about to begin an exciting journey, learning a
powerful programming language. Java is cross-platform,
object-oriented, network-based, and multimedia-ready. After
its inception in May 1995, Java quickly became an attractive
language for developing Internet applications. This chapter
introduces Java and its programming features, followed by a
simple example of Java applications.
2
The History of Java
Java was developed by a team led by James Gosling at Sun
Microsystems, a company best known for its Sun workstations.
Originally called Oak , it was designed in 1991 for use in
embedded consumer electronic applications. In 1995, renamed
Java , it was redesigned for developing Internet
applications. Java programs can be embedded in HTML pages
and downloaded by Web browsers to bring live animation and
interaction to Web clients.
The power of Java is not limited to Web applications, for it
is a general-purpose programming language. It has full
programming features and can be used to develop standalone
applications. Java is inherently object-oriented. Although
many object-oriented languages began strictly as procedural
languages, Java was designed from the start to be object-
oriented. Object-oriented programming (OOP) is a popular
programming approach that is replacing traditional
procedural programming techniques.
NOTE: One of the central issues in software
development is how to reuse code. Object-
oriented programming provides great flexibility,
modularity, clarity, and reusability through
method abstraction, class abstraction, and class
inheritance—all of which you’ll learn about in
this book.
Characteristics of Java
Java has gained enormous popularity. Java's rapid rise and
wide acceptance can be traced to its design and programming
features, particularly its promise that you can write a
program once and run the program anywhere. As stated in the
Java-language white paper by Sun, Java is simple , object-
oriented , distributed , interpreted , robust , secure ,
architecture-neutral , portable , high-performance ,
multithreaded , and dynamic . Let’s analyze these often-used
buzzwords.
Java Is Simple
No language is simple, but Java is a bit easier than the
popular object-oriented programming language C++, which was
the dominant software-development language before Java. Java
is partially modeled on C++, but greatly simplified and
improved. For instance, pointers and multiple inheritance
often make programming complicated. Java replaces the
multiple inheritance in C++ with a simple language construct
called an interface , and eliminates pointers.
3
Java uses automatic memory allocation and garbage
collection, whereas C++ requires the programmer to allocate
memory and collect garbage. Also, the number of language
constructs is small for such a powerful language. The clean
syntax makes Java programs easy to write and read. Some
people refer to Java as "C++--" because it is like C++, but
with more functionality and fewer negative aspects.
Java Is Object-Oriented
Computer programs are instructions to the computers. You
tell a computer what to do through programs. Without
programs, a computer is an empty machine. Computers do not
understand human languages, so you need to use computer
languages to communicate with computers. There are over one
hundred programming languages. The most popular languages
are:
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional
Code)
Pascal (Named for Blaise Pascal)
Ada (Named for Ada Lovelace)
C (Whose developer designed B first)
Visual Basic (Basic-like visual language developed by
Microsoft)
Delphi (Pascal-like visual language developed by
Borland)
C++ (an object-oriented language, based on C)
Each language was designed with a specific purpose. COBOL
was designed for business applications and now is used
primarily for business data processing. FORTRAN was designed
for mathematical computations and is used mainly for numeric
computations. BASIC, as its name suggests, was designed to
be learned and used easily. Pascal was designed to as a
simple structural programming language. Ada was developed at
the direction of the Department of Defense, and is mainly
used in defense projects. C is popular among system software
projects like writing compilers and operating systems.
Visual Basic and Delphi are for rapid application
development. C++ is the C language with object-oriented
features.
All of these languages except C++ are known as procedural
programming languages . Software systems developed using
procedural programming languages are based on the paradigm
of procedures. Object-oriented programming models the real
world in terms of objects. Everything in the world can be
modeled as an object. A circle is an object, a person is an
object, and a window’s icon is an object. Even a mortgage
can be perceived as an object. A Java program is object-
4
oriented because programming in Java is centered on creating
objects, manipulating objects, and making objects work
together.
Part I, “Fundamentals of Programming,” introduces primitive
data types and operations, control statements, methods, and
arrays. These are the fundamentals for all programming
languages. You will learn object-oriented programming in
Part II, “Object-Oriented Programming.” Object-oriented
programming provides great flexibility, modularity, and
reusability. For years, object-oriented technology was
perceived as elitist, requiring a substantial investment in
training and infrastructure. Java has helped object-oriented
technology enter the mainstream of computing. Its simple,
clean syntax makes programs easy to write and read. Java
programs are quite expressive in terms of designing and
developing applications.
Java Is Distributed
Distributed computing involves several computers working
together on a network. Java is designed to make distributed
computing easy. Since networking capability is inherently
integrated into Java, writing network programs is like
sending and receiving data to and from a file. For example,
Figure 1.1 shows three programs running on three different
systems; the three programs communicate with each other to
perform a joint task.
***Insert Figure 1.1 (Same as Figure 1.1 in the 3 rd Edition,
p6)
Figure 1.1
Java programs can run on different systems that work
together.
Java Is Interpreted
You need an interpreter to run Java programs. The programs
are compiled into Java Virtual Machine code called bytecode .
The bytecode is machine-independent and can run on any
machine that has a Java interpreter, as shown in Figure 1.2.
***Insert Figure 1.2 (Same as Figure 1.2 in the 3 rd Edition,
p7)
Figure 1.2
The Java interpreter executes Java bytecode.
Usually, a compiler, such as a C++ compiler, translates a
5
Zgłoś jeśli naruszono regulamin