📄 ch54.htm
字号:
(<TT>http://java.sun.com/ tutorial/java/index.html</TT>) is an excellent introduction
to the language. The Java Developer's Kit is free if you are using it for personal
reasons only, but if you plan to publish Java-based Web pages, you might need a license.
Details are included with the JDK.</P>
<P>When installing the JDK, make sure that the path to the Java executables is in
your path. Even better, link the executables to <TT>/bin</TT>. The most common errors
Java programmers make while learning the language have to do with case sensitivity
and class use. Java, like UNIX, is case sensitive, so developers must be careful
to ensure that their code is correct. Class usage follows C++ methods, and it must
be adhered to properly. A good Java book is a must for learning the language, and
there are many books on learning Java.</P>
<P>For JavaScript, all you need is a JavaScript-enabled browser (such as Netscape
Navigator 2.02 or higher), a standard ASCII editor or an editor that can save in
ASCII, and a TCP/IP stack to communicate with other machines over the Internet or
an intranet.
<H3 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">The Java Language</FONT></H3>
<P>From the programmer's point of view, Java is a very stripped-down object-oriented
(OO) language. The principles of OO programming are suitable in most cases to client-server
applications, so using OO for Java made sense. By staying with OO, Java avoided the
need to set up procedural language structures, all of which cause code bloat to the
point that Java would not be nearly as fast. Also, by avoiding procedural language
principles, Java designers can ignore many of the startup and initialization aspects
of programming, again making the source code smaller and faster. Learning Java is
not especially difficult. If you know an OO language such as C++, you will probably
find Java easier to learn than if you've worked only in procedural languages such
as C or BASIC.</P>
<P>Java may be an OO language, but it's not strict about it. SmallTalk, for example,
is considered a pure OO language because all aspects of SmallTalk are dealt with
as either objects or messages, and all data are object classes. Java doesn't go quite
as far, primarily to avoid an overly large language. Java implements all the simple
C data types (integers, floating points, and characters) outside of the OO method,
but everything else is object-based. This is both a benefit, because it leads to
smaller, faster code, and a problem, because it means you can't subclass Java data
objects.</P>
<P>As mentioned earlier, Java is interpreted. When you develop Java applications,
you write the Java code as you would with any other language, then pass it through
a Java processor to produce the binary object file. The binary object file, called
a class file in Java, is not directly readable, but it can be transmitted faster
than the source code from which it was derived. When a Java-equipped browser receives
the class file, it runs the class file through the Java interpreter, which decodes
and executes the class file instructions. In one sense, Java is both a compiler and
an interpreter, in much the same way as many early languages that produced pseudo-code
requiring a runtime module were. The Java client that decodes the class file performs
a conversion from the generic instructions contained in the class file to machine-specific
instructions for the client's hardware and operating system.</P>
<P>Programming in the Java language takes a little while to become comfortable. The
code often seems like a hybrid of C and C++, and experience with both is handy. Because
Java is a fairly simple language, however, even nonprogrammers can pick it up with
a little practice. The most difficult aspects of learning Java for most people are
the need to understand object-oriented design and programming, and the somewhat awkward
syntax requirements (these are also familiar to C and C++ programmers). For example,
here's a simple Java program:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"> // HelloWorldApp.java
class HelloWorldApp {
public static void main (String args[]){
system.out.println("Hello World!");
}
}
</FONT></PRE>
<P>This program (called <TT>HelloWorldApp</TT>) defines a single method called <TT>main</TT>
that returns nothing (<TT>void</TT>) and consists of a single Java instruction to
print the message <TT>Hello World!</TT> The first line in the code is a comment.
To compile this source code into a class file, you invoke the Java compiler with
the following command line:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">javac HelloWorldApp.java
</FONT></PRE>
<P>The compiler then grinds away and produces a class file (<TT>HelloWorldApp.class</TT>).
This class file can be run in a Java-equipped browser or from the command-line client
with the command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">java HelloWorldApp
</FONT></PRE>
<P>which instructs Java to load and execute the <TT>.class</TT> file.</P>
<P>As you probably expect, Java is a lot more complicated than that in real life,
but if you have programmed before, you will see the similarities to other languages
(especially C and C++). The Java language is quite rich. You might need a couple
of weeks to wade through it, becoming familiar with its nuances as you go, but for
most people, Java is much more easily learned than other programming languages.
<H3 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">JavaScript and HTML</FONT></H3>
<P>You embed JavaScript commands inside HTML documents (see Chapter 53, "HTML
Programming Basics," for more details on HTML) by enclosing them in the HTML
tags <TT><SCRIPT></TT> and <TT></SCRIPT></TT>. The general syntax for
a JavaScript program inside an HTML document looks like this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"><SCRIPT language="JavaScript">
JavaScript statements
</SCRIPT>
</FONT></PRE>
<P>The language option inside the <TT><SCRIPT></TT> tag is optional, but it
is a good idea to use it to make sure that the browser knows what is incoming. If
you want to load the JavaScript from a URL, you need to embed the URL in the <TT><SCRIPT></TT>
tag like this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"><SCRIPT language="JavaScript" src="http://www.where.com">
</FONT></PRE>
<P>If the JavaScript source is embedded in the HTML file, you can leave off the <TT>SRC</TT>
component. For example, here's a simple JavaScript applet in some HTML code (which
has been trimmed down to the essentials):<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF"><HTML>
<HEAD>
...
<SCRIPT language="JavaScript">
alert("Welcome to my Web site!");
</SCRIPT>
</HEAD>
</HTML>
</FONT></PRE>
<P>The <TT>alert()</TT> function in JavaScript displays the message in a window with
an exclamation mark icon next to it. This is usually used to catch your attention
when you try to do something critical or illegal, or something that might cause problems.
JavaScript's functions, as you can see, are much like those in C. You can define
a complete function within a JavaScript file, similar to in a C program.</P>
<P>If you are calling a file with the JavaScript source in it from your HTML code,
the convention is to name the file with the <TT>.js</TT> filetype at the end (<TT>sample.js</TT>,
for example). This is because several applications, including MIME, already recognize
the <TT>.js</TT> filetype and can handle them properly.</P>
<P>We don't have the space here to go into details about JavaScript programming,
but many good books have been published on the subject, including Web Programming
with Java and Teach Yourself Java in 14 Days (both Sams.net, 1995).
<H3 ALIGN="CENTER"><A NAME="Heading7<FONT COLOR="#000077">Summary</FONT></H3>
<P>We've taken a quick look at Java and JavaScript, both of which are available for
Linux platforms in both server and client versions. Programming both Java and JavaScript
requires a bit of past programming experience, but if you've programmed before, you
can use them both to add a lot of features to Web pages and HTML documents. Give
them a try! After all, the software is free.
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -