Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


CHAPTER 37
Developing Java Applets

by Mike Morgan

In this chapter
Basic Language Constructs 982
Leveraging Java Classes and Packages 984
Installing the JDK 984
Building Your First Applet 990
Running the Applet 992
Step by Step Through the Code 996
Life Cycle of an Applet 997
Sample Applets 1001
HelloApplet as a Standalone Application 1021

Basic Language Constructs

This section introduces Java language basics that will aid you in the creation of applets. Tables 37.1 through 37.4 summarize Java’s basic language constructs. Table 37.1 shows the data types available for variables and constants. Table 37.2 shows the operators available for manipulating those variables and constants. Table 37.3 shows the major statements of the language, used for controlling the flow of execution through the program. The last table, Table 37.4, shows three ways to add comments to your program.

Table 37.1 Basic Language Constructs (Java Types)

Type Example Notes

boolean boolean flag=false; A Java boolean is just true or false. It cannot be cast to char or int.
char char c[]={‘A’,’\uu42',’C’}; A Java char is a 16-bit Unicode character. Use the Java String class to manage Unicode strings.
byte byte b=0x7f; 8-bit signed integer (–127 .. 127).
short short s=32767; 16-bit signed integer (–32,768 .. 32,767).
int int i=2; 32-bit signed integer.
long long l=2L; 64-bit signed integer. Use the suffix L for a long (decimal) literal.
float float x=2.0F; 32-bit IEEE754 number. Use the suffix F for a float literal.
double double d=2.0; 64-bit IEEE754 number (15 significant digits).


NOTE:  Some languages, such as JavaScript and Visual Basic, are weakly typed—they enable the programmer to make new variables on-the-fly and to freely interchange numbers, text, or other values.

Java is a strongly typed language. The programmer must explicitly declare the “type” of each variable. You can’t arbitrarily mix or inter-convert types.

To deal with strong typing, focus on classes rather than primitive data types.

By thinking at this higher level—the class level—you will need fewer primitive types and they will be less likely to interact with each other in troublesome ways. In addition, you will end up with simpler, more robust programs.


The operators in Table 37.2 are arranged in order of precedence. The compiler will treat the expression 2 + 2 * 2 ^ 2 as 2 + (2 * (2 ^ 2)), for example, executing 2 XOR 2 first, 2 * the result next, and so on.

Table 37.2 Basic Language Constructs (Java Operators)

Operator Description

. member selection
[] array subscript
() parentheses/function call
++, -- auto-increment/auto-decrement
*, /, % arithmetic: multiply, divide, modulo
+, -, arithmetic: add, subtract
<<, >>, >>> bitwise: shift left, arithmetic shift right, and logical shift right
<=, <, >, >= equality: less than or equal to, less than, greater than, greater than or equal to
==, != equality: equal to, not equal to
&, |, ^, ~ bitwise: AND, OR, Exclusive Or (XOR), and NOT
&&, ||, ! logical: AND, OR, and NOT
? : conditional expression
= simple assignment
*=, /=, %=, +=, -=, &=, assignment with operation
|=, ^=, <<=, >>=, >>>=

In your own Java programs, use parentheses liberally. Compare how much easier it is to read 2 + (2 * (2 ^ 2)) than 2 + 2 * 2 ^ 2. Not only is the first version clearer, but using parentheses rather than relying on the default precedence hierarchy will also help you avoid a common source of defects.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.