java-01.pdf
(
24 KB
)
Pobierz
Java Tutorial
Introduction
java-01.fm
Object-Oriented Programming
The task of composition of operations is often considered the heart of the art of programming. ... However,
it will become evident that the appropriate composition of data is equally fundamental and appropriate.
— N. Wirth, Algorithms + Data Structures = Programs, 1976.
Object-oriented design is based on the following concepts:
Abstraction:
the decision to concentrate on properties which are shared by many objects or situations in the real
world, and to ignore the differences between them.
Representation
: the choice of a set of symbols to stand for the abstraction (i.e., data structures)
Manipulation:
the rules for transformation of the symbolic representation as a means of predicting the effect of
similar manipulation in the real world (i.e., functions and procedures).
Axiomatization:
the rigorous statement of those properties which have been abstracted from the real world and
which are shared by manipulation of the real world and the symbols which represent it.
— C. A. R. Hoare, Notes on Data Structuring, 1972.
It has taken 20+ years to bring these ideas into wide-spread use by programmers. Now, everyone wants to do object-
oriented programming. Why?
Object-oriented programming is now common practice among professional software engineers, and programming
languages like C++ and Java allow the programmer to express solutions to computational problems in a variety of
different ways.
However, the important thing is not the choice of language, but being able to think about a problem in an object-
oriented manner, and then use a specific language and its object-oriented features to implement a well-engineered
solution that solves the problem such that a program executes
correctly
,
safely
, and
efficiently
.
Greg Lavender
Slide 1 of 8
6/7/99
Java Tutorial
Introduction
java-01.fm
Objects + Classes + Inheritance = OO Programs
object-based
+ classes
Ada, Modula-2
+ inheritance & subtyping
class-based
Clu, C with classes
object-oriented
Simula, Smalltalk, CLOS, Eiffel, Modula-3, C++, Java
Objects ---
provide a conceptual framework for thinking about typed data and typed operations that correspond to a
real world object that one wishes to model computationally.
The term ‘object’ will also be used to refer to the run-
time state of a conceptual object.
Classes ---
are used to declare a new
type
with compile-time (static) attributes of data and operations (methods) that
a set of objects, or
instances
of that class, will have at run-time
.
Inheritance ---
is a mechanism that is used to define new (sub-)types by specializing existing types. Inheritance is a
compile-time (static) mechanism in a language like Java.
Templates --
allow compile-time (static) parameterization of a class by other types and constant values.
Greg Lavender
Slide 2 of 8
6/7/99
Java Tutorial
Introduction
java-01.fm
A Brief History of C++ and Java
C++ was “invented” by Bjarne Stroustrup in the early 1980s at Bell Labs. The original intent of the language was to
have the efficiency and portability of C, but borrowing object-oriented ideas from Simula (i.e., the class) and CLU
(generic functions and types). The main features were to be strong typing, encapsulation using classes, and
inheritance, as improvements on C. Features like templates and exception handling came later.
C++ became commercially popular and as things usually go, a committee was formed to standardize the language.
C++ is now a complex programming language with lots of features and several “traps and pitfalls” for the novice
programmer. It has taken several years for C++ compiler developers to implement the language as defined by the
ANSI C++ standard. Today, there are still some C++ compilers that do not yet accept the full language defined by the
C++ language standard. There is also a severe lack of useful freely available class libraries, which has been a
drawback to using C++.
This is a persisten and frustrating problem that has limited C++’s success, and
these problems inspired the inventors of Java to produce a new object-oriented language.
Having said that, C++ is a very practical and useful programming language in some contexts. In this course, you will
learn mostly about its useful features, its traps and pitfalls, and enough about the complex features to make
intelligent decisions of when to use or avoid them and how to write professional quality programs.
Java, on the other hand, is an interesting “new” approach to object-oriented programming. In fact, Java steals most of
its language ideas from other languages. It’s object model is very similar to Modula-3 while its syntax looks like C/
C++. The hype surrounding this language is unprecedented because of the success of the web. It is a “simpler”
language than C++ (and was meant to be), which means it lacks some of the features that C++ programmers find
useful. Some people predict the death of C/C++ and the world-wide dominance of Java.
Java was originally called Oak, and was developed by James Gosling (inventor of Gosling Emacs) and others at Sun
Microsystems that initially were trying to build an special operating system and programming language for “video on
demand” applications for your TV sets in the early 1990s. This technology went no where, and Sun tried to kill off the
Oak project. The group survived, with the help of Sun co-founder Bill Joy (author of csh among other things in BSD
Unix), and re-targeted Oak at the Internet as a more flexible programming language for “programming the net”. Once
it started to take off, Sun renamed Oak to Java, because another company had already trademarked Oak as a name.
The popularity and hype over Java, which is afterall just a programming language, is unprecedented. However, the
language is allowing programmers to achieve wide--spreadcode reuse, which was not achieved with C++.
Greg Lavender
Slide 3 of 8
6/7/99
Java Tutorial
Introduction
java-01.fm
Introduction to Java
Java’s basic language syntax is similar to C/C++, but there are many differences too. Java is
strongly typed
like
C++, meaning that a Java compiler detects type inconsistencies at compile-time. A Java compiler translates Java
source code into Java
bytecodes
, which are instructions to a Java
virtual machine
(Java VM).
A Java VM executes a Java program by
interpreting
the byte codes as virtual machine instructions, which in turn
cause the VM to execute real machine instructions. Interestingly, the java interpreter is mostly written in Java! So,
when you execute the interpreter, your running a java program. There is a small run-time part that is written in C,
and the rest of it is implemented in Java. For example, the Java compiler is written in Java! This is also the case
with other interpreted programming languages, such as Smalltalk, Lisp, Scheme, Dylan, ML, and others.
source code
interpreter (VM)
bytecode compiler
bytecode file
Hello.java
javac
Hello.class
java
Java bytecodes are machine independent. This means that you can compile Java source code on one hardware
platform and transfer the bytecodes to another platform and execute them using a Java VM that has been ported to
that platform. This machine independence of the bytecode instructions is what makes Java suitable for
“programming the Internet.” Recently,
native-code
Java compilers have been developed for performance reasons.
You can send Java bytecodes around the Internet as data in the form of an
applet
in a web page or as a
application/
java
MIME bodypart in an e-mail message. Sending code around the net in this manner is called providing
executable content
. Java is not the only language that is used for executable content. You can use most interpreted
languages in this manner, as long as there is a virtual machine available to interpret the data that is received. In just
two years, Java has been become language of choice for providing executable content on the Web.
Greg Lavender
Slide 4 of 8
6/7/99
Java Tutorial
Introduction
java-01.fm
Running the Java Compiler (javac) and Interpreter (java)
On CS Dept. Solaris systems, the java compiler, interpreter, and class sources are in the directory /lusr/java. See the
README file in that directory. The directory /lusr/java/bin has the java compiler and interpreter, so you need to have
that in your PATH. To run the compiler, you just give the compiler the name of a .java file.
% javac Hello.java
% java Hello
Hello, world
Note that you do not specify the .class extension when you execute a class file. In addition, if you do not
name your .java file with the same name as the public class inside the file (i.e., public class Hello), the
compiler will complain.
On Windows 95/NT, you can either run the Sun Java Developer Kit (JDK) for Windows, or you can use a commercial
Java development environment (e.g., Visual J++, CodeWarrior). It’s up to you to decide which one you like best. You
can download the Sun JDK from:
http://java.sun.com/
JDK 1.1.8 is a stable version for Solaris, Windows 95 & NT, Linux, Mac, etc. JDK 1.2.1 is very new. If you plan to use
Visual J++, check the following URL for up-to-date information on Microsoft Java SDKs:
http://www.microsoft.com/visualj
CodeWarrior info is available a the URL:
http://www.metrowerks.com/
Also see my CS371 webpage for a number of URLs to Java sites. Download a copy of the Java Coding Conventions
from:
http://java.sun.com/docs/codeconv/
Other documentation is available at:
http://java.sun.com/docs/index.html
Greg Lavender
Slide 5 of 8
6/7/99
Plik z chomika:
Dest90
Inne pliki z tego folderu:
java-00.pdf
(4 KB)
java-01.pdf
(24 KB)
java-02.pdf
(30 KB)
java-04.pdf
(20 KB)
java-05.pdf
(18 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin