Applets
and Applications
Java
applications fall into two
main groups: applets and applications.
Applets, as you have
learned, are Java programs that are downloaded over the World Wide Web and
executed by a Web browser on the reader’s machine. Applets depend on a
Java-capable browser in order to run (although they can also be viewed using a
tool called the appletviewer.
Java applications are
more general programs written in the Java language. Java applications don’t require
a browser to run, and in fact, Java can be used to create most other kinds of
applications that you would normally use a more conventional programming
language to create.
HotJava itself
is a Java application.
A
single Java program can be an applet or an application or both, depending on
how you write that program and the capabilities that program uses. Throughout
this lecture, you’ll be writing mostly HotJava applications; then you’ll apply
what you’ve learned to write applets in next lecture. If you’re eager to get
started with applets, be patient. Everything that you learn while you’re
creating simple Java applications will apply to creating applets, and it’s
easier to start with the basics before moving onto the hard stuff. You’ll be
creating plenty of applets in next lectures.
Creating
a Java Application
Let’s start by
creating a simple Java application: the classic Hello World example that all
language books use to begin.
As with all
programming languages, your Java source files are created in a plain text editor,
or in an editor that can save files in plain ASCII without any formatting
characters. On Unix, emacs, ped, or vi will work; on Windows, Notepad or DOS
Edit are both text editors.
Fire up your editor
of choice, and enter the Java program shown in Listing 1.1. Type this program,
as shown, in your text editor. Be careful that all the parentheses, braces, and
quotes are there.
Listing
1.1. Your first Java application.
1: class HelloWorld {
2: public static void main (String args[]) {
3: System.out.println(“Hello World!”);
4: }
5: }
!! Warning:
The numbers before each line are part of the listing and not part
of the program; they’re there so I can refer to specific line numbers when I
explain what’s going on in the program. Do not include them in your own file.
This program has two
main parts:
All the program is enclosed in a class
definition—here, a class called HelloWorld. The body of the program (here, just the one
line) is contained in a routine called main(). In Java applications, as in a C or C++ program, main()
is the first routine that is run when the program is executed. You’ll
learn more about both these parts of a Java application as the class
progresses.
Once
you finish typing the program, save the file. Conventionally, Java source files
are named the same name as the class they define, with an extension of .java. This file should therefore be called HelloWorld.java.
Now, let’s compile the source file using the Java compiler. In
Sun’s JDK, the Java compiler is called javac.
To compile your Java program, Make sure the javac program is in
your execution path and type javac followed by the name
of your source file: javac
HelloWorld.java
Note:
In these examples, and in all the examples throughout these
lectures, we’ll be using Sun’s Java compiler, part of the JDK. If you have a
third-party development environment, check with the documentation for that
program to see how to compile your Java programs.
The
compiler should compile the file without any errors. If you get errors, go back
and make sure that you’ve typed the program exactly as it appears in Listing
1.1.
When
the program compiles without errors, you end up with a file called HelloWorld.class, in
the same directory as your source file. This is your Java bytecode
file. You can then run that bytecode file using the Java interpreter. In the
JDK, the Java interpreter is called simply java.
Make sure the java
program is in your path and type java followed
by the name of the file without the .class extension: java HelloWorld
Analysis
1
If your program was
typed and compiled correctly, you should get the string “Hello World!” printed
to your screen as a response.
Note:
Remember, the Java compiler and the Java interpreter are different
things. You use the Java
compiler (javac) for your Java source
files to create .class files, and you use the Java interpreter (java)to
actually run your class files.
Creating
a Java Applet
Creating
applets is different from creating a simple application, because Java applets
run and are displayed inside a Web page with other page elements and as such
have special rules for how they behave. Because of these special rules for
applets in many cases (particularly the simple ones), creating an applet may be
more complex than creating an application.
For
example, to do a simple Hello World applet, instead of merely being able to
print a message, you have to create an applet to make space for your message and
then use graphics operations to paint the message to the screen.
Note:
Actually, if you run the Hello World application as an applet, the
Hello World message prints to a special window or to a log file, depending on
how the browser has screen messages set up. It will not appear on the screen
unless you write your applet to put it there.
In
the next example, you create that simple Hello World applet, place it inside a
Web page, and view the result.
First, you set up an environment so that your Java-capable browser
can find your HTML files and your applets. Much of the time, you’ll keep your
HTML files and your applet code in the same directory. Although this isn’t
required, it makes it easier to keep track of each element. In this example,
you use a directory called HTML that contains all the files you’ll need.
mkdir HTML
Custom patches online represent a convenient and efficient way for individuals and businesses to personalize and enhance their garments, accessories, or promotional items. Through user-friendly platforms, customers can easily navigate the design process, choosing patch sizes, uploading custom artwork, and selecting color schemes. The online customization experience often includes real-time previews, allowing users to visualize their designs before placing an order. This seamless process ensures accuracy and satisfaction with the final product.
ReplyDeleteTop enterprise selection consultants are indispensable partners for organizations navigating the intricate process of choosing the right solutions to meet their complex needs. Renowned for their expertise and industry knowledge, these consultants provide invaluable guidance throughout the selection process, ensuring that businesses make informed decisions aligned with their objectives and budgets. They offer comprehensive assessments of organizational requirements, conduct thorough vendor evaluations, and facilitate effective decision-making processes. Moreover, top enterprise selection consultants stay abreast of the latest industry trends and technologies, enabling them to recommend innovative solutions that drive growth and competitive advantage.
ReplyDelete